I am having trouble with Taylor approximation to e^x at 0
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
ag
il 3 Ott 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 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!