PLEASE HELP ME!! :(
Mostra commenti meno recenti
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
1 Commento
F Valencia
il 19 Mar 2021
Risposta accettata
Più risposte (1)
Mathieu NOE
il 19 Mar 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 Commenti
F Valencia
il 19 Mar 2021
Mathieu NOE
il 22 Mar 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)
- ...
Categorie
Scopri di più su Performance and Memory in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
