I am having issued with my for loop taking the variable that i have set to be [1:1:n] but when i run my script it turns my answer into a scular in stead of a matrix.
here is what i have any help would be great thanks.
clc;
clear;
close all;
n = input('Please give number for the total number of terms in the taylor series: ');
x = input('Please give a value for "x": ');
approxValue = 0;
% Initial value of approxValue.
for k = [0:1:n];
approxVakue = (approxValue + k);
approxValue = sum(x.^k/factorial(k));
% Gives the approx value of e^x as a taylor series
end
disp('approxValue =')
disp((approxValue))
disp('e^x =')
disp(exp(x))

1 comentario

Dillan Masellas
Dillan Masellas el 8 de Abr. de 2016
How can I perform this operation without using the power function?

Iniciar sesión para comentar.

 Respuesta aceptada

Matt Fig
Matt Fig el 6 de Sept. de 2012
Editada: Matt Fig el 6 de Sept. de 2012

0 votos

Use this loop instead:
for k = 0:n
approxValue = (approxValue + x.^k/factorial(k));
% Gives the approx value of e^x as a taylor series
end
I don't know what approxVakue is supposed to be doing in your code??
And what do you expect to be a matrix? Are you trying to save each term? If so:
approxValue = zeros(1,n+1);
for k = 0:n
approxValue(k+1) = x.^k/factorial(k);
end
approxValuesum = sum(approxValue); % Holds the estimate
This could also be done without FOR loops...

6 comentarios

SonOfAFather
SonOfAFather el 6 de Sept. de 2012
approxValue should be very close to the actual answer of e^x in the end when we use a large number of terms right now i am getting a huge differance in my answers.
Thank you that solved this freaking issue i am struggeling to learn this new language, Matlab, I find that i can do the computations, but knowing how to word it so that Matlab will accapt it is really kicking my butt. I can't thank you enough for you assistance.
Matt Fig
Matt Fig el 6 de Sept. de 2012
You're welcome!
SonOfAFather
SonOfAFather el 6 de Sept. de 2012
i am still somewhatconfused as to why in the workspace the value of k is given as a number and not a vector? is it a result of the opperation be run and therefore at the end it is a number?
Matt Fig
Matt Fig el 6 de Sept. de 2012
k is the loop index. Thus k takes values of 0,1,...n one at a time, each time through the loop. Why would you need k at the end??
SonOfAFather
SonOfAFather el 6 de Sept. de 2012
thank you i understand now. It wasn't that i needed to know that the value of k it was that it didn't do what i thought it would do. i thought that the value of k would still stay in vector form, but your explaination corrected my thought process. thank you.
SonOfAFather
SonOfAFather el 12 de Sept. de 2012
In the end i was told that"n" should have been "n-1" in
for k = 0:n should have read k = 0:n.
the explaination i was given was that each time to loop processed through it needed to be one less.
thanks for you help. just thought i would tell you how i was corrected.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 6 de Sept. de 2012

Comentada:

el 8 de Abr. de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by