Error Using integral - Help
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Can someone offer a solution as to why my code does not work for plotting my fourier series representation>
clc;
clear all;
close all;
T = 0.004; %time period of the waveform
F0=100000;
syms t
f = (16*F0*t/(3*T))*((t>=0)&(t<(T/8)))+ ((-40*F0/(3*T))*(t-T/8)+2*F0/3)*((t>=T/8)&(t<T/4))+((40*F0/(3*T))*(t-3*T/8)+2*F0/3)*((t>=T/4)&(t<(3*T/8)))+(-16*F0*(t-T/2)/(3*T))*((t>=3*T/8)&(t<T/2));
pi = 3.14;
sum1 = 0; %for 5 sum
a0 = (2/T)*int(f,t,-T/2,T/2);
for j = 1:5
an = (2/T)*int(f*cos((2*j*pi*t)/T), t,-T/2,T/2);
bn = (2/T)*int(f*sin((2*j*pi*t)/T), t,-T/2,T/2);
sum1=sum1+(an*cos((2*j*pi*t)/T)+bn*sin((2*j*pi*t)/T));
end
fplot(f,[0,T])
hold on
fplot((sum1+a0/2), [0 T],'-o')
sum2 = 0; %for 10 sum
for j = 1:10
an = (2/T)*int(f*cos((2*j*pi*t)/T), t,-T/2,T/2);
bn = (2/T)*int(f*sin((2*j*pi*t)/T), t,-T/2,T/2);
sum2=sum2+(an*cos((2*j*pi*t)/T)+bn*sin((2*j*pi*t)/T));
end
hold on
fplot((sum2+a0/2), [0 T],'-h')
sum3 = 0; %for 10 sum
for j = 1:50
an = (2/T)*int(f*cos((2*j*pi*t)/T), t,-T/2,T/2);
bn = (2/T)*int(f*sin((2*j*pi*t)/T), t,-T/2,T/2);
sum3=sum3+(an*cos((2*j*pi*t)/T)+bn*sin((2*j*pi*t)/T));
end
hold on
fplot((sum3+a0/2), [0 T],'-s')
sum4 = 0; %for 10 sum
for j = 1:500
an = (2/T)*int(f*cos((2*j*pi*t)/T), t,-T/2,T/2);
bn = (2/T)*int(f*sin((2*j*pi*t)/T), t,-T/2,T/2);
sum4=sum4+(an*cos((2*j*pi*t)/T)+bn*sin((2*j*pi*t)/T));
end
hold on
fplot((sum4+a0/2), [0 T],'-*')
legend('Original','N=5', 'N=10', 'N=50', 'N=500');v
0 comentarios
Respuestas (1)
Walter Roberson
el 2 de Mayo de 2019
You are using the form expression * comparison . If you were working numerically, then that would produce 0 if the comparison was false, and produce the expression if the comparison were true. However, your comparisons are symbolic in t, and symbolic comparisons do not produce 0 or 1 as their result: they produce the non-numeric values TRUE and FALSE that cannot be converted to double.
The way you construct those expressions causes the int() to take much longer than they need to... and your plotting would fail when it tried to convert the expressions to numbers.
What you can do is wrap each of those comparisons as piecewise(comparison,1,0) . You can write small function for that purpose, such as
PW = @(v) piecewise(v, 1, 0);
expression*PW(comparison) + expression2*PW(comparison2)
2 comentarios
Walter Roberson
el 2 de Mayo de 2019
You can improve the performance of your for j = 1:500 loop substantially. Instead of int() with each separate numeric j value, you can, before the loop,
syms J
an = (2/T)*int(f*cos((2*J*pi*t)/T), t, -T/2, T/2);
bn = (2/T)*int(f*sin((2*J*pi*t)/T), t, -T/2, T/2);
and then your loop can use subs(an,J,j) instead of re-calculating an, and subs(bn,J,j) instead of re-calculating bn.
Ver también
Categorías
Más información sobre Calculus en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!