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!