Matlab - Weird answer with all zeroes

  • Thread starter eurekameh
  • Start date
  • #1
210
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?
 

Answers and Replies

  • #2
lewando
Homework Helper
Gold Member
1,349
135
Because your result is insanely large? What were you expecting? No way to help unless you share what you have done.
 
  • #3
Simon Bridge
Science Advisor
Homework Helper
17,857
1,654
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.
 
  • #4
210
0
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:
  • #5
lewando
Homework Helper
Gold Member
1,349
135
Try using a smaller step size, like h=.001--to compare this with your manual solution, what did you get?
 
  • #6
Simon Bridge
Science Advisor
Homework Helper
17,857
1,654
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:

Related Threads on Matlab - Weird answer with all zeroes

  • Last Post
Replies
5
Views
2K
Replies
1
Views
4K
  • Last Post
Replies
1
Views
1K
Replies
1
Views
3K
  • Last Post
Replies
7
Views
3K
Replies
3
Views
533
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
4
Views
2K
Top