MATLAB: Plotting summation using for loop

for loopplotsummation notation

How would I plot values of i for 1,2,3,4,5 individually on the same graph?
x = -pi:0.1:pi;
ye = cos(x);
n = length(x);
summe = 0.0;
for i = 1:n
summe = summe +((-1).^(i)).*((x.^(2.*i))./(factorial(2.*i)));
end
hold on
plot (x, summe);
plot (cos(x), ye)

Best Answer

  • In previous question, as I told you, if you want your n vector as n=1:5, then you have to set your x vector as
    x=-pi:pi/2:pi
    and
    n=1:length(x)
    but it will be not wise to do this since when you plot, the figure will be a triangular shape. Therefore, you have to sample as many points as you can,
    x = -pi:0.1:pi
    is a wise choice.