problem with for and function inside other function

조회 수: 4 (최근 30일)
camilo
camilo 2014년 9월 14일
답변: Mike Hosea 2014년 9월 26일
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에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by