Filter löschen
Filter löschen

Numeircal integral with external function and external parameter

6 Ansichten (letzte 30 Tage)
Saeid
Saeid am 12 Jan. 2019
Bearbeitet: Saeid am 13 Jan. 2019
I would like to calculate an integral whereas the integrand is a separate external function. Consider as an example that I have in my main script:
N=5;
I = integral(fn,0,Inf,'RelTol',1e-8,'AbsTol',1e-13)
Where N is a parameter of the integrand that appears in the separate function named: fn.m that has the following form:
function FUN= fn(x)
FUN=@(x) (x.^N).*exp(-x).*sin(x)
end
But when I do this I see the following error:
Undefined function or variable 'N'.
How can I help Matlab take this external parameter into the integrand function?

Akzeptierte Antwort

madhan ravi
madhan ravi am 12 Jan. 2019
Bearbeitet: madhan ravi am 12 Jan. 2019
EDITED
N = 5;
F = fn(x,N); % function call
I = integral(F,0,Inf,'RelTol',1e-8,'AbsTol',1e-13)
function FUN = fn(x,N) % function definition
FUN = @(x) (x.^N).*exp(-x).*sin(x);
end
Gives:
I =
-15.0000
  7 Kommentare
Walter Roberson
Walter Roberson am 13 Jan. 2019
function f3=FF3(x)
f3(1)=integral(fint1(x(1),x(2)),0,1)-2.333
f3(2)=integral(fint2(x(1),x(2)),0,pi)
end
function FUN1= fint1(x1,x2)
FUN1=@(y) x1*y.^2+x2
end
function FUN2= fint2(x1,x2)
FUN2=@(y) x1*sin(x2*y)
end
Saeid
Saeid am 13 Jan. 2019
Bearbeitet: Saeid am 13 Jan. 2019
Thank you Walter!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by