Need help to plot a function
Show older comments
Hi, I'm a beginner Matlab user and I'm trying (for the first time) plot a function. But for some reason the program do not plot. I cant find the error.
N = 6;
tau = [-150:150]/100;
Xquad = ones(1,301)*1/(16*(pi^2));
A = zeros(1,301);
for n = 1:1:N
A = (4*sin(n*(pi)./(4*tau)))/((n^2)*(pi^2)*(((4*(pi^2) - (n^2)*(4*(pi^2)))./(tau^2))^2 + (16/100)*(n^2)*(4*(pi^2)./(tau^2)))) ;
Xquad = Xquad+ A;
end
Xrms = sqrt(Xquad);
plot(tau, Xrms);
Answers (1)
Friedrich
on 8 Apr 2011
Hi Pedro,
I think you missed some dots in your code, e.g.
tau^2
will not work since tau is a vector. You have to use
tau.^2
instead. The following modified code works for me
N = 6;
tau = [-150:150]/100;
Xquad = ones(1,301)*1/(16*(pi^2));
A = zeros(1,301);
for n = 1:1:N
A = (4*sin(n*(pi)./(4*tau)))./((n^2)*(pi^2)*(((4*(pi^2) - (n^2)*(4*(pi^2)))./(tau.^2)).^2 + (16/100)*(n^2)*(4*(pi^2)./(tau.^2)))) ;
Xquad = Xquad+ A;
end
Xrms = sqrt(Xquad);
plot(tau', Xrms');
I hope this helps,
Friedrich
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!