I am having trouble with Taylor approximation to e^x at 0
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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?
0 comentarios
Respuestas (1)
ag
el 3 de Oct. de 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)
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!
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!