Create an Exponential Function using Taylor Series
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to write a function whose input is a row vector of numbers (which I am calling x) and whose output is the row vector e^x. e^x is calculated by the summation of (x^n)/n! where n=0,1,2,... I need to terminate the sequence when either the maximum value of (x^n)/n! is less than 0.01 or n reaches a maximum number of 50. I cannot use the inbuilt exponential function in Matlab. I am not quite sure where to start so all help is appreciated!
0 commentaires
Réponses (2)
pfb
le 11 Avr 2015
Hi John,
of course If x is your vector, y=exp(x) is what you are looking for. I'm not sure why you can't use the builtin exponential function. Is this an assignment?
Anyway, if you want to go through the Taylor series, of course you should truncate it at some cutoff value N. Recalling that sum works along the columns of a matrix, one possible way of doing what you need is something like
y = sum((xx.^nn)./fnn)
where xx, nn and fnn are matrices suitably built from the (row) vector x and the column vectors n=(0:N)' and fn = factorial(n). You can use matrix multiplication or the builtin function meshgrid to build the matrices.
I hope this useful. Francesco
2 commentaires
Manivanna Boopathi Arumugam
le 27 Oct 2020
If you want to stop when the maximum value of (x^n)/n! is less than 0.01, it should be
"while abs(x.^n/factorial(n))>0.01". Not "while abs(x.^n/factorial(n))<=0.01"
pfb
le 12 Avr 2015
Modifié(e) : pfb
le 12 Avr 2015
Ok John, you want to use a loop instead of using the matrix capabilities of matlab.
Imho your code contains both coding [C] and math [M] mistakes.
1) [C] x seems to be a vector, but is not defined (matlab should complain).
2) [M] n starts from 1. The series starts from 0.
3) [M] It seems to me that 0.001 is your cutoff for the contribution to the series. You never mentioned that before. Anyway, the inequality is wrong. You want to consider the large terms, not the small ones.
4) [C] if x is a vector, then the clause in your while statement is weird, because it involves a vector. No actually it is plain wrong. I expect that "x.^n/factorial(n)" produces an error because you cannot divide vectors (only rdivide, i.e. elementwise). If you do not get that error, chances are that your while loop is never entered. That explains why you always get 0. Your y goes through the code unscathed. Even if you use ./ instead of /, you are likely to get wrong results, because the check is performed over all the elements of the vector. If at least one does not meet it, it is as if none of them meets it.
I suggest some debugging. Printout things using "disp" or "fprintf". Or make use of the debugging features of the matlab editor. It's all in the documentation.
If you want to use loops, you should have one external (for) loop over the elements of x, and one internal (while) loop over n. No need of the vector n here.
Otherwise you can use the strategy I suggested yesterday. If you want to enforce the cutoff, you can do like this
ee=(xx.^nn)./fnn;
ee = ee.*(find(ee>0.001));
y = sum(ee);
This sets to 0 all the elements smaller than 0.01 (the result of find is a matrix with ones where the condition is met, and zeros otherwise). Of course you should make sure that at least one element in each column of ee (i.e. each element of x) has a 0, otherwise you are not enforcing the cutoff correctly.
I'm going to give you some more hints about this. If the length of x is L and your cutoff on n is N,
xx should be a (N+1)*L matrix, whose rows are all copies of x
nn should be a (N+1)*L matrix, whose columns are all copies of n
fnn should be a (N+1)*L matrix, whose columns are all copies of fn
Take a look at meshgrid, or suitably use the matrix product with column or row vectors of ones.
0 commentaires
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!