Matlab - Weird answer with all zeroes

  • Thread starter Thread starter eurekameh
  • Start date Start date
  • Tags Tags
    Matlab Weird
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB function that produces unexpected output, specifically a result consisting of all zeroes and a large scaling factor. Participants explore the reasons behind this output, focusing on numerical methods for solving a system of differential equations using Euler's method.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant notes that the output includes a large scaling factor, questioning why MATLAB presents it this way.
  • Another participant suggests that the result may be due to the user's programming choices and that further details are needed to provide assistance.
  • A different viewpoint indicates that nonsensical results can arise from exceeding the computer's numerical capabilities or from programming errors, recommending a change in units.
  • The original poster describes their attempt to solve a system of differential equations using Euler's method and expresses confusion over receiving zeroes instead of expected values.
  • One participant advises using a smaller time step to mitigate the issue of results growing infinitely, suggesting a specific smaller step size for comparison.
  • Another participant recommends using a rational number for the time step to avoid potential issues with binary representation, and shares modifications to the code to improve the numerical solution.
  • Concerns are raised about the stability of the numerical method, with one participant noting that results can diverge for low values of the number of time steps.

Areas of Agreement / Disagreement

Participants express varying opinions on the causes of the unexpected output and potential solutions. There is no consensus on the best approach to resolve the issue, as multiple strategies are proposed.

Contextual Notes

Participants highlight limitations related to time step size and numerical stability in Euler's method, noting that larger time steps can lead to divergent results.

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:

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
5K