NB. the Cn that I'm using is actually an integration computed by matlab so it's slightly different than the one that is shown on my paper.
PLEASE HELP ME!! :(
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
F Valencia
am 19 Mär. 2021
Kommentiert: Mathieu NOE
am 22 Mär. 2021
My homework is due in 48 hours and I've been trying for about 5-6 hours and It doesn't work at all.
So I need to plot the equation that somehow looks like this
by using this f(x)
by using this f(x)
this is what I've done so far yet it doesn't even show up the sigma line
Can anyone help me pls? I need to redo with n=20,50,and 100 as well
fplot(@(x) x,[0,2],'m-')
hold on
fplot(@(x) 6,[2,pi],'m-')
axis([0 pi 0 10])
y=6-10/pi+(((2*n*sin(2*n) - 2*sin(n)^2)/n^2 - (6*(sin(2*n) - sin(pi*n)))/n)/(pi/2 + sin(2*pi*n)/(4*n))).*cos(n*x)
for i=1:1:10;
f(i)=subs(y,n,i);
end
g=sum(f);
x=linspace(0,pi,1000);
plot(x,g)
hold off
Akzeptierte Antwort
Steven Lord
am 19 Mär. 2021
I would turn your y into a function. Rearranging your code a bit:
fplot(@(x) x,[0,2],'m-')
hold on
% SL: I used repmat to avoid a warning
fplot(@(x) repmat(6, size(x)),[2,pi],'m-')
axis([0 pi 0 10])
% SL: I converted y from an expression (which would have used a symbolic variable x, I assume)
% to a function handle and added ... to break the expression visually in the middle
%
% I took the (6-10/pi) part out of the function since you only add that once
% at the end in the expression on the paper.
%
% This is the main modification to your code. Most of the rest is just rearrangement.
y=@(x, n) (((2*n*sin(2*n) - 2*sin(n)^2)/n^2 - ...
(6*(sin(2*n) - sin(pi*n)))/n)/(pi/2 + sin(2*pi*n)/(4*n))).*cos(n*x);
% SL: I moved this up
x=linspace(0,pi,1000);
% Start f with the first term (6-10/pi)
f = 6-10/pi;
% Now add each term to it
for i=1:1:10;
f = f + y(x, i);
end
% No need to sum anymore, the + inside the for loop took care of that
plot(x,f)
hold off
Weitere Antworten (1)
Mathieu NOE
am 19 Mär. 2021
hello Felika
don't panic !!
the code does exactly what you wrote by hand. It just a matter to know how to make a loop work with matlab;
the loop will compute each term of the sum (there was no mistake or bug in your formula) and we add them at each iteration, it's the key point
nota also I use k (and not n) as running index (n is the final value)
then only have to add 6-10/pi to the sum and here you are !!
hope you feel better now !
fplot(@(x) x,[0,2],'m-')
hold on
fplot(@(x) 6,[2,pi],'m-')
axis([0 pi 0 10])
% init
n = 10;
x=linspace(0,pi,500);
sum = 0 ; % init summation to zero
for k=1:1:n
Cn = (((2*k*sin(2*k) - 2*sin(k)^2)/k^2 - (6*(sin(2*k) - sin(pi*k)))/k)/(pi/2 + sin(2*pi*k)/(4*k)));
sum = sum + Cn.*cos(k*x);
end
y = 6-10/pi+sum;
plot(x,y)
hold off

2 Kommentare
Mathieu NOE
am 22 Mär. 2021
hello Felika
well , sometimes the difference between a code that works and not is very tiny.
Basically the way you coded the function is ok except that to perform a for loop you have to be aware of a few key points like :
- do not forget to index variables (that obviously are function of the index value)
- do not use the same variable name for the index and the max value of it (like : for n = 1:n )
- do not forget to initialize counters or variables that increment in the loop
- use preallocation (better)
- make sure that dimensions are matchning when you do product / division of vectors and matrixes (as usual)
- ...
Siehe auch
Kategorien
Mehr zu Performance and Memory finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
