Effacer les filtres
Effacer les filtres

Trying to use summation notation in a for loop

1 vue (au cours des 30 derniers jours)
James Crowe
James Crowe le 26 Oct 2017
Commenté : Birdman le 26 Oct 2017
Hi I'm trying to estimate a cos graph using summation between 1:1, 1:2, 1:3, 1:4 and 1:5. How would I plot each of these individually?
Could someone help me out please! Thank you
x = -pi:0.1:pi;
ye = cos (x);
n = 5;
summe = 0.0;
for i = 1:n
summe = summe +((-1).^(i)).*((x.^(2.*i))./(factorial(2.*i)));
end
plot (x, summe);

Réponse acceptée

Birdman
Birdman le 26 Oct 2017
Modifié(e) : Birdman le 26 Oct 2017
Firstly, this code will give an error since X vector is 1x63 and n is 1x5. There will be a size mismatch. You have to correct this. Use the following code.
x = -pi:0.1:pi;
ye = cos (x);
n = length(x);%has to be same size with x
i = 1:n;
for k = 1:n
summe = summe +((-1).^(i)).*((x.^(2.*i))./(factorial(2.*i)));
end
plot(x,summe)
  2 commentaires
James Crowe
James Crowe le 26 Oct 2017
It's for some coursework I've been told to use a for loop
Birdman
Birdman le 26 Oct 2017
I have corrected it.

Connectez-vous pour commenter.

Plus de réponses (1)

KSSV
KSSV le 26 Oct 2017
x = -pi:0.1:pi;
ye = cos (x);
n = length(x);
i = 1:n;
summe = 0.0;
for k = 1:n
summe = summe +((-1).^(i)).*((x.^(2.*i))./(factorial(2.*i)));
end
plot (x, summe);

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by