I am having trouble with Taylor approximation to e^x at 0
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
ag
2024-10-3
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!