How to define an integral function of a piecewise function

2 vues (au cours des 30 derniers jours)
Chiara Boetti
Chiara Boetti le 12 Mar 2020
Commenté : Chiara Boetti le 16 Mar 2020
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!

Réponses (1)

Abhisek Pradhan
Abhisek Pradhan le 16 Mar 2020
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 commentaire
Chiara Boetti
Chiara Boetti le 16 Mar 2020
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

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by