Taylor Series of e^x
    30 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Joseph Melo Manalo
 el 21 de Mzo. de 2020
  
    
    
    
    
    Respondida: Mohamed Hakim
 el 21 de Mayo de 2021
            The program calculates e^x by adding terms of the series and stopping when the absolute value of the term that was added last is smaller than 0.0001. Use a while-end loop, but limit the number of passes to 30. If in the 30th pass the value of the term that is added is not smaller than 0.0001, the program stops and displays a message that more than 30 terms are needed.
clear
clc
fprintf('Solving e^x using Taylor Series\n');
x=input('x = ');
n=30; 
i=0;
f=1;
while i<n || i==n
    f=f+(x.^i)/factorial(i);
    i=i+1;
end
y=f-1
This is where I am stuck, I don't know how to limit the n to 30 and how can I make a condition that if the value of the term is smaller than 0.0001 it will stop.
Any help would be appreciated, Thank you!
1 comentario
  Walter Roberson
      
      
 el 21 de Mzo. de 2020
				while term is in range && iterations is in range 
  Calculate a term
  accumulate term into total 
  increment iterations
Respuesta aceptada
  Subhamoy Saha
      
 el 21 de Mzo. de 2020
        clear
clc
fprintf('Solving e^x using Taylor Series\n');
x=input('x = ');
n=30; 
i=0;
f=1;
while i<=n
    last_term=(x.^i)/factorial(i);
    if (x.^i)/factorial(i)<0.0001
        msgbox('Last term is smaller than 0.0001 and hence stopped')
        break
    elseif i==n && last_term>0.0001
        msgbox('More steps needed')
        break
    end
    f=f+last_term;
    i=i+1;
end
y=f-1
4 comentarios
Más respuestas (2)
  Mohamed Hakim
 el 20 de Mayo de 2021
        function [ts]=newton(x,n)
  i=1;
  ts=1;
  while i<n || i==n
      ts=ts+(x.^i)/factorial(i);
      i=1+i;
  end
end
1 comentario
  Walter Roberson
      
      
 el 20 de Mayo de 2021
				Is there a reason to write
i<n || i==n
instead of 
i<=n
?
  Mohamed Hakim
 el 21 de Mayo de 2021
        y= @(x) 2*x^2-5*x+3;
x1=input("enterfirst number");
x2=input("enterfirst number");
if f(x1)*f(x2)==0
    disp("no");
end
0 comentarios
Ver también
Categorías
				Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



