problem with for and function inside other function

1 Ansicht (letzte 30 Tage)
camilo
camilo am 14 Sep. 2014
Beantwortet: Mike Hosea am 26 Sep. 2014
hello, i do not handle very much with matlab so i'm asking for help...
i have this for...
for i=1:n1
t=0:0.001:Tp;
p=@(t)((t<=1).*(0)+((t>1)&(t<=1.4)).*(100*sin(2*pi()*(t-1)/T))+(t>1.4).*(0));
cos1=@(t)cos(2*pi()*i*(t)/Tp);
sin1=@(t)sin(2*pi()*i*(t)/Tp);
a0=(1/Tp)*quad(p,0,Tp);%constant
pp1=@(t)a0;
qa1=quad(p.*cos1,1,1.4);
a1(i)=(2/Tp)*qa1;
qb1=quad(p.*sin1,1,1.4);
b1(i)=(2/Tp)*qb1;
pp1=pp1+a1(i)*cos(2*pi()*i*t/Tp)+b1(i)*sin(2*pi()*i*t/Tp);
end
n1 and Tp are defined before the for. The error is with the qa1=quad(p.*cos1,1,1.4); i think i don't know how to multiply functions
also, i dont know if the last line "pp1=pp1+a1(i)*cos(2*pi()*i*t/Tp)+b1(i)*sin(2*pi()*i*t/Tp);" is corectly wrote
sorry for bad english, hope you can help me

Akzeptierte Antwort

Mike Hosea
Mike Hosea am 26 Sep. 2014
Do not use QUAD. Use INTEGRAL. If you don't have INTEGRAL because your version of MATLAB is too old, use QUADGK.
Functions cannot be multiplied in MATLAB. To define a new function that is the product of the return values of two other functions, p and cos1, you should write
qa1 = integral(@(t)p(t).*cos1(t),1,1.4)
You can store that function in a variable if it seems clearer.
pc = @(t)p(t).*cos1(t);
qa1 = integral(pc,1,1.4);
.

Weitere Antworten (1)

Youssef  Khmou
Youssef Khmou am 15 Sep. 2014
From your description , the error occurs when you calculate the integral of pxcos1 from 1 to 1.4, that is the quad function, you have to mention what the error says, is times Matrix dimensions must agree? if it is the case then you need adjust the length of p or cos1.

Community Treasure Hunt

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

Start Hunting!

Translated by