How to define an integral function of a piecewise function

2 次查看(过去 30 天)
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