I want to define a function in the integral form
f(x)=integral(g(z), z=0, z=x)
as an example:
for which I wrote:
fun = @(z) z^2+3*z-1;
x=linspace(0,1,100);
y = integral(fun,0,@(x) x)
plot(x,y)
But Matlab returned the following error:
Error using integral (line 85)
A and B must be floating-point scalars.
Error in IntegralFunction (line 8)
y = integral(fun,0,@(x) x)
Is it possible at all to do this in Matlab?

 채택된 답변

Torsten
Torsten 2018년 9월 6일

2 개 추천

fun=@(z)z.^2+3*z-1;
F=@(x)integral(fun,0,x);
F(10)

댓글 수: 5

Saeid
Saeid 2018년 9월 6일
Danke, Torsten!
Saeid
Saeid 2018년 9월 7일
편집: Saeid 2018년 9월 7일
Hi again Torsten! Actually, my aim to define a function like this was for the final result (in this case F(x)=x^3/3+3*x^2/2-x) to be a coefficient in a differential equation. Something like: y"+F(x).y'=y"+(x^3/3+3*x^2/2-x)*y'=0 Do you know if that is possible in Matlab?
Torsten
Torsten 2018년 9월 7일
Yes, it's possible just the way I showed you above. What exactly is the problem when you want to use it in your context ?
I tried the following format but it didn't work:
tspan = [0 5];
y0 = 0;
[t,y] = ode45(@(t,y) DYDT(t,y) , tspan, y0);
plot(t,y,'-o')
function dydt=DYDT(t,y)
fun=@(z)z.^2+3*z-1;
dydt=@(y)integral(fun,0,y)
end
Hey, I am also stuck with the same proble, did you find the solution? Can you please help me out with this?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

질문:

2018년 9월 6일

댓글:

2023년 4월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by