Matlab - Weird answer with all zeroes

  • Thread starter Thread starter eurekameh
  • Start date Start date
  • Tags Tags
    Matlab Weird
AI Thread Summary
The discussion revolves around a Matlab function that outputs a result with all zeroes and a large factor, 1.0e+147. The issue arises from the numerical instability of the explicit Euler method when solving a system of differential equations, leading to infinite growth in results due to a large time interval. Participants suggest using a smaller time step to avoid overflow and improve accuracy. Adjustments to the code are recommended, including setting a specific number of time steps to ensure convergence. The consensus is that careful selection of parameters is crucial for accurate numerical solutions in Matlab.
eurekameh
Messages
209
Reaction score
0
I programmed a function that outputted this weird answer that looks like:
ans =
1.0e+147 *

Columns 1 through 16

0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000

Columns 17 through 32

-0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000

I've noticed there's a '1.0e+147 * ' in the answer. Does anybody know why Matlab is putting in this factor?
 
Physics news on Phys.org
Because your result is insanely large? What were you expecting? No way to help unless you share what you have done.
 
MatLab gave that result because of what you programmed it to do.
Cannot be more exact without knowing what you did.

You will get nonsense results if you program nonsence in - and also if the program is otherwise reasonable but it's requirements exceeds the abilities of your computer... in this case, probably you have exceeded your computer's ability to store real numbers. Change your units.
 
I'm trying to numerically solve this system for x1 and x2, using the forward (explicit) Euler's formula:
dx1/dt = 298x1+598x2
dx2/dt = -299x1-599x2

Here is my code, with time step h = 0.1:

h = 0.1;
t = 0:h:10;
x1(1) = 1; %initial conditions
x2(1) = 0;
for i = 1:length(t)
x1(i+1) = x1(i) + h*(298*x1(i) + 598*x2(i)); %Euler's forward formula
x2(i+1) = x2(i) + h*(-299*x1(i) - 599*x2(i));
end

When solving this system by hand, I am not getting the results that I'm getting from Matlab, namely 0's.

Edit: I just found out what the problem is. The solution grows infinitely, and because my time interval (t = 0:h:10) is too large, x1(10) and x2(10) is a huge number. If I kept my time interval small, this problem wouldn't exist because my results wouldn't be too large either.

How can I work my way around this, considering that I need to plot x1 vs. t and x2 vs. t?
 
Last edited:
Try using a smaller step size, like h=.001--to compare this with your manual solution, what did you get?
 
I was going to suggest a smaller step size - but one which is not an irrational number in binary. ##1/2^6## or so.

I'm going to suggest the following modifications:
Code:
a=0; b=10; N=512;
h = (b-a)/N;
t = a:h:b;
x1(1) = 1; %initial conditions
x2(1) = 0;
for i = 1:length(t)
x1(i+1) = x1(i) + h*(298*x1(i) + 598*x2(i)); %Euler's forward formula
x2(i+1) = x2(i) + h*(-299*x1(i) - 599*x2(i));
end

plot(t,x1(1:length(t)),"o",n,x2(1:length(t)),"x")

This codes gives you N time steps, for N+1 data points in t, for N+2 data points for x1 and x2.
Exploring - the results blow up for low values of N. I needed N=2048 to get the series to converge.
That sort of thing is normal for these kinds of calculations: if your time-step is too large, you break the model.
 
Last edited:
Back
Top