I am very new to Matlab, and have been trying to create function to do something quite specific. I have two functions, one which finds an approximation of e^x for n iterations using the Taylor series expansion, and another which then uses this function to find the smallest value of n for which the error is less than 1. The code for the taylorexp function is:
function taylorexp(order,iterations)
y=0;
for i=0:iterations
a=(order.^i)./factorial(i);
y=y+a;
end
disp(y)
And the code for the error function is:
function taylorexperror(erroracceptability,order)
answer=exp(order);
iterations=1;
y=0;
taylorexp(order,iterations)=y;
while abs(answer-y)>erroracceptability
y=taylorexp(order,iterations);
iterations=iterations+1;
end
disp(iterations)
end
When I run this say as taylorexp(1,10) I get this error message:
Attempted to access taylorexp(10,2); index out of bounds because size(taylorexp)=[10,1].
Error in taylorexperror (line 7)
y=taylorexp(order,iterations);
I am unsure how to proceed and would appreciate your help.
Thanks, Andrew

 Accepted Answer

Stephen23
Stephen23 on 14 Dec 2015
Edited: Stephen23 on 14 Dec 2015
You are trying to access an array element that does not exist. Basically you have something like this:
>> X = [1,2,3]
>> X(4)
Index exceeds matrix dimensions.
Of course this causes an error, because there is no fourth element in X.
However it could be that there is some confusion with the variable taylorexp: although you define it as a variable on this line
taylorexp(order,iterations)=y;
perhaps you thought that that calls your taylorexp function?
I would recommend that you do not give functions and variables the same name (or use the names of any MATLAB functions). If it is supposed to be a function call, what do you imagine that
taylorexp(order,iterations)=y;
should do?

6 Comments

Ah yes, I thought that would call the function. I was hoping that it would run taylorexp using the "order" value inputted in the error function and an iteration of zero, and then in the while loop the iteration would increase by one every time. How do you call the function? Thank you!
Stephen23
Stephen23 on 14 Dec 2015
Edited: Stephen23 on 14 Dec 2015
Make sure that taylorexp is either a local function, or is on your MATLAB path. Then you can simply call it using this syntax:
taylorexp(order,iterations)
The confusing bit is the =y: what do you think this does? You cannot allocate a value (e.g. y) to a function call. You can allocate the output of a function call to value, did you mean to do this?:
y = taylorexp(order,iterations)
Thanks, that's really helpful - now I get a new error! The code is the same as I posted before, but I've allocated the output of the taylorexp function call to a value (y) by putting y=taylorexp(order,iterations).
I get this:
Error using taylorexp
Too many output arguments.
Error in taylorexperror (line 5)
z=taylorexp(order,iterations);
Any ideas? Thanks, Andrew
Stephen23
Stephen23 on 17 Dec 2015
Edited: Stephen23 on 17 Dec 2015
You are calling function taylorexperror with an output argument, yet you did not define that function to have any output arguments (see 1>0, thus the error message):
function taylorexperror(erroracceptability,order)
If you want it to have output arguments, then you need to name them explicitly in the function definition:
function yourOutputHere = taylorexperror(erroracceptability,order)
For more information use your favorite internet search engine. These pages took me 0.5 seconds to find:
Ah OK I finally understand now. Apologies for being so dense.
You should never apologize for learning something!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!