I'm using this function:
function [ tf,ca ] = Implicit_Trapizoidal_Method( ca0, t0, tf, dt )
t=t0+dt;
while t<=tf
ca=ca0+(dt/2)*(f_function(ca0)+f_function(successive_substitution(ca0,dt,ca0,5)));
ca0=ca;
t0=t;
t=t+dt;
end
tf=t0;
end
when I enter the command (for example):
[ tf,ca ] = Implicit_Trapizoidal_Method( 0.0095, 0, 1,0.1)
I receive a logical answer where tf=1 just like I expected, but when I enter the command:
[ tf,ca ] = Implicit_Trapizoidal_Method( 0.0095, 0, 0.3,0.1)
I receive tf=0.2 for some reason... It happens with more numbers like 1.4, 3 and so on.
I would love to get some help, thanks.

 Akzeptierte Antwort

Torsten
Torsten am 17 Jun. 2016

0 Stimmen

The reason is "Floating Point Arithmetic": Maybe in the first case tf=0.9999999999 while in the second case tf=0.2000000000001.
You should substitute the while loop by a simple for loop: don't use t0, tf and dt as input parameters to Implicit_Trapizoidal_Method, but t0, tf and n (number of subintervals in t0-tf to be considered). Then no such floating point confusion can occur.
Best wishes
Torsten.

Gefragt:

am 17 Jun. 2016

Kommentiert:

am 17 Jun. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by