Borrar filtros
Borrar filtros

Why my code doesn't work?

2 visualizaciones (últimos 30 días)
liakoyras
liakoyras el 2 de Jun. de 2017
Comentada: liakoyras el 2 de Jun. de 2017
I m trying to make a program that calculates cosh(x) using an approximation. The error between the calculated value and the real value must be less than e-10. If i try to run the code for small numbers or very large the results are correct, but for x=50 for example, the variable termSum becomes NaN and everything is ruined. What am i doing wrong?
clc;
clear;
x = input('Give x to calculate cosh(x): ');
disp(' ')
y1 = (exp(x)+exp(-x))/2;
terms = zeros(1, 1500);
err = 1;
i=0;
while err>10^(-10)
terms(i+1) = (abs(x).^i)/factorial(i);
termSum = sum(terms);
ex = termSum^sign(x);
y2 = (ex + ex^(-1))/2;
err=abs(y2-y1);
i=i+1;
end;
disp(['cosh(', num2str(x),')=', num2str(y2)])

Respuesta aceptada

Guillaume
Guillaume el 2 de Jun. de 2017
Editada: Guillaume el 2 de Jun. de 2017
I've not tested your code but to me it does not look that it's going to produce the right result for any value.
For a start, I don't understand why you use a series expansion of the exponential rather than a straightforward expansion of cosh directly.
edit: removed my own nonsense
You're getting NaN for x = 50 because at the 182th iteration, 50^182 is Inf, and as explained on the factorial page, the factorial of any number above 170 is also Inf, so for i = 182, factorial is Inf. Inf/Inf is Nan.
There's no point predeclaring 1500 terms since as stated you can't get a factorial above 170 terms. You may as well put a hard stop to your calculation when i reach 170.
  5 comentarios
Guillaume
Guillaume el 2 de Jun. de 2017
It's not just matlab, it's inherent to floating point numbers.
You may be able to achieve this precision with the symbolic toolbox but I suspect that it would slow your algorithm to a crawl. Storing a number of 1e21 magnitude with a precision of 1e-10 is kind of meaningless.
You really need to adjust your precision to the magnitude of the result.
liakoyras
liakoyras el 2 de Jun. de 2017
Thanks a lot, unfortunately I have to deal with this precision.

Iniciar sesión para comentar.

Más respuestas (1)

Kelly Kearney
Kelly Kearney el 2 de Jun. de 2017
Editada: Kelly Kearney el 2 de Jun. de 2017
I haven't given any thought to the algorithm you show, though a quick test does seem to indicate it converges to cosh for small numbers. But as Guillaume points out, both the numerator and denominator in your terms equation get pretty big pretty quickly:
nterm = 200;
ii = 1:nterm;
x = 50
tnum = abs(x).^ii;
tden = factorial(ii);
terms = tnum./tden;
You can examine the values here:
[tnum' tden' terms']
The denominator becoming infinite (after 172 terms, due to limitations in factorial) isn't actually a problem, although at that point the extra term adds 0 to the sum ( x/Inf = 0 ) and the convergence stops. If the numerator becomes infinite first (e.g. x = 1000), you'll start getting Inf in your terms ( Inf/x = Inf ), and if both become infinite, you'll get NaNs ( Inf/Inf = NaN ).

Categorías

Más información sobre Sparse Matrices 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!

Translated by