I am having trouble with Taylor approximation to e^x at 0

1 view (last 30 days)
function y=myexp(x,n);
%this is my first function
%y is the n-th order Taylor approximation to exp(x)
%x is a scalar; n is positive integer
y=1;
term=1;
for k=1:n %n is a scalar
term=term*x/k;
y=y+term;
end
I am taking this error.
Not enough input arguments.
Error in myexp (line 8) for k=1:n %n is a scalar
what is wrong with that?

Answers (1)

ag
ag on 3 Oct 2024
Hi Erol,
The error you are encountering arises because the variable "n" has not been initialized. As a result, the line
for k = 1:n
generates an error since "n" is undefined.
To resolve this issue, you need to ensure that "n" is properly initialized before it is used in the loop. To do this is you will have to run the script by including a call to the function, and pass the necessary values when invoking the function.
The below code snippet demonstrates how to achieve this:
y = myexp(1, 10)
y = 2.7183
function y=myexp(x,n)
y=1;
term=1;
for k=1:n %n is a scalar
term=term*x/k;
y=y+term;
end
end
Hope this helps!

Categories

Find more on Loops and Conditional Statements 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!