why plot result is different from fplot's
이전 댓글 표시
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?
채택된 답변
추가 답변 (1개)
Jesus Sanchez
2020년 3월 4일
0 개 추천
For plot, you wrote that x is defined between -pi and pi.
For fplot, you can see the reason in its reference page
댓글 수: 6
pooneh shabdini
2020년 3월 4일
pooneh shabdini
2020년 3월 4일
Jesus Sanchez
2020년 3월 5일
편집: Jesus Sanchez
2020년 3월 5일
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
2020년 3월 5일
Jesus Sanchez
2020년 3월 5일
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
2020년 3월 5일
카테고리
도움말 센터 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
