PLEASE HELP ME!! :(

7 vues (au cours des 30 derniers jours)
F Valencia
F Valencia le 19 Mar 2021
Commenté : Mathieu NOE le 22 Mar 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)
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 commentaire
F Valencia
F Valencia le 19 Mar 2021
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.

Connectez-vous pour commenter.

Réponse acceptée

Steven Lord
Steven Lord le 19 Mar 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
  1 commentaire
F Valencia
F Valencia le 19 Mar 2021
Thank you so much for the details!! It's about 2 AM here and I checked this out before I go to bed and see someone has answered my question. I'm so touched and thanks once again.

Connectez-vous pour commenter.

Plus de réponses (1)

Mathieu NOE
Mathieu NOE le 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 commentaires
F Valencia
F Valencia le 19 Mar 2021
Hi Mathieu!! thank you so much for your concern and help, I wish I could accept everyone's answer as it really helps as well. Your concern really touches my heart and im so releived now :))
Btw, may I ask the reason that my code didn't work cuz you mentioned that there was no mistake in it?
have a nice day :)))
Mathieu NOE
Mathieu NOE le 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)
  • ...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by