Sorry, this is a very simple question I'm sure but I just can't work it out. I'm trying to plot the error of my function that ascertains e^x for a value of x. However, when I try and plot the vectors containing the relevant information I get a load of errors.
My M-file:
function T = findexp(x,n)
%findexp, function to evaluate e^x
e=zeros(1,n);
T(1)=1;
e(1)=abs(T(1)-exp(x))/x;
for i = 2:n+1
T(i) = T(i-1)+x^(i-1)/(factorial(i-1));
e(i) = abs((T(i)-exp(x))/(x));
if i == n+1
fprintf(1,'T(%d) = %1.15e\n',x,T(i));
end
end
p=1:1:n+1;
y=e(p);
plot(p,y)
However, what I get is the output:
EDU>> findexp(1,25) T(1) = 2.718281828459046e+000 ??? Input argument "n" is undefined.
Error in ==> error at 4 e(i) = abs((findexp(x,n)-exp(x))/x);
Error in ==> clo at 20 error(nargchk(1, 3, nargin));
Error in ==> cla at 29 clo(ax, extra{:});
Error in ==> newplot>ObserveAxesNextPlot at 134 cla(ax, 'reset',hsave);
Error in ==> newplot at 83 ax = ObserveAxesNextPlot(ax, hsave);
Error in ==> findexp at 17 plot(e,p)
What am I doing wrong? Thanks for your help in advance

댓글 수: 2

the cyclist
the cyclist 2012년 1월 19일
It would be really helpful if you spent some time formatting your question better, using the Code button better.
Harry
Harry 2012년 1월 19일
Of course, sorry I'm a bit of a novice here, thanks for the advice I will from now on

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2012년 1월 19일

0 개 추천

You defined your own file error.m, conflicting with MATLAB's error() routine.

댓글 수: 1

Harry
Harry 2012년 1월 19일
Thanks so much, as soon as I deleted that it worked fine, thank you

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

the cyclist
the cyclist 2012년 1월 19일

0 개 추천

I put the following lines of code into a file called "findexp.m"
function T = findexp(x,n)
%findexp, function to evaluate e^x
e=zeros(1,n);
T(1)=1;
e(1)=abs(T(1)-exp(x))/x;
for i = 2:n+1
T(i) = T(i-1)+x^(i-1)/(factorial(i-1));
e(i) = abs((T(i)-exp(x))/(x));
if i == n+1
fprintf(1,'T(%d) = %1.15e\n',x,T(i));
end
end
p=1:1:n+1;
y=e(p);
plot(p,y)
and then called them like this:
>> findexp(1,25)
That worked fine for me. It seems to me that the problem is not in your function findexp(), but rather in how you call it. It looks from your error message that you have not defined the second input argument, n, when you call findexp().

댓글 수: 1

Harry
Harry 2012년 1월 19일
Hi, thanks for your help but I'm still a bit confused, I just followed what you did exactly but I still get the same problem, whereas if I take the plot(p,y) bit out it works fine, but obviously doesn't plot the graph. Any thoughts?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by