How to define an integral function of a piecewise function

조회 수: 2 (최근 30일)
Chiara Boetti
Chiara Boetti 2020년 3월 12일
댓글: Chiara Boetti 2020년 3월 16일
Hello!
I want to define an integral function but I have a problem with the integral function. This is my code:
syms s t
figure(1)
P = [0.5 1.25 2 4.5 5 7.25];
for i=1:4
subplot(4,2,i)
hold on
a = @(s) (s-P(i))/(P(i+1)-P(i));
b = @(s) (P(i+2)-s)/(P(i+2)-P(i+1));
f(i) = piecewise(s<=P(i),0, P(i)<s<=P(i+1),a(s), P(i+1)<s<=P(i+2),b(s), s>P(i+2),0);
fplot (f(i),[0,P(4)])
hold off
grid
subplot(4,2,i+1)
hold on
F = @(t) integral(f(i), 0, t);
fplot(F(t),[0,P(4)])
hold off
grid
end
In particular, there is no problem with the function f, however Matlab returns the following error:
Error using integral (line 82)
First input argument must be a function handle.
Error in example_f_int>@(t)integral(f(i),0,t) (line 19)
F = @(t) integral(f(i), 0, t);
Error in example_f_int (line 20)
fplot(F(t),[0,P(4)])
There is a way to solve this error and finaly plot the F funciont?
Thanks, any help would be appreciated!

답변 (1개)

Abhisek Pradhan
Abhisek Pradhan 2020년 3월 16일
Intergral function take a function handle as its first argument. Adding @(arg) Infront of piecewise () may work.
Refer the following link for more details on how to use integral function.
  댓글 수: 1
Chiara Boetti
Chiara Boetti 2020년 3월 16일
Hi!
As you suggest, I've changed the code with
f = @(s,i) piecewise(s<=P(i),0, P(i)<s<=P(i+1),a(s), P(i+1)<s<=P(i+2),b(s), s>P(i+2),0);
fplot (f(s,i),[0,P(4)])
and
F = @(t) integral(f(s,i), 0, t);
However there is still an error:
Error using integral (line 82)
First input argument must be a function handle.
Error in example_f_int>@(t)integral(f(s,i),0,t) (line 17)
F = @(t) integral(f(s,i), 0, t);
Error in example_f_int (line 18)
fplot(F(t),[0,P(4)])
What did I do wrong?
Thanks

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

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by