why plot result is different from fplot's
Show older comments
function [ Y ] = cosin( n,x)
s=size(x,2);
Y=zeros(1,s);
for i=1:n
Y=Y+((((-1)^(i-1))*(x.^((2*i)-2)))/factorial((2*i)-2));
end
end
I used this function once in plot and then in fplot:
x=-pi:pi/10:pi;
Y=cosin(600,x);
plot(x,Y)
result of plot was this shape:

syms x
fplot(cosin(600,x))
the result of fplot was this:

why plot just show this small part of cos(x) and not more?
Accepted Answer
More Answers (1)
Jesus Sanchez
on 4 Mar 2020
0 votes
For plot, you wrote that x is defined between -pi and pi.
For fplot, you can see the reason in its reference page
6 Comments
pooneh shabdini
on 4 Mar 2020
pooneh shabdini
on 4 Mar 2020
Jesus Sanchez
on 5 Mar 2020
Edited: Jesus Sanchez
on 5 Mar 2020
hello Pooneh,
what do you mean that that is not the shape of a cosine function? As fas as I see it, its exacty the same. Periodic behaviour, having value y=1 at x=0 and -1 at x= pi = 3.14 etc etc
EDIT: I just realized that you are using cosin instead of cos. You could ocmpare your function with matlab function for cosine is cos(x). Just plot them at the same graph using hold on.
pooneh shabdini
on 5 Mar 2020
Jesus Sanchez
on 5 Mar 2020
I saw the result in matlab and it is indeed strange. The Taylor expansions resulting of Y gives "NaN" as a result for values of x that are not cointained within [-pi, pi].
The only mistake that I can see is that you have not written i-1 everywhere, so you are not implementeing correctly the function. I will give it more thought later!
pooneh shabdini
on 5 Mar 2020
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!