problem with for and function inside other function

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

采纳的回答

Mike Hosea
Mike Hosea 2014-9-26
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);
.

更多回答(1 个)

Youssef  Khmou
Youssef Khmou 2014-9-15
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.

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by