1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab - Weird answer with all zeroes

  1. Nov 24, 2012 #1
    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?
     
  2. jcsd
  3. Nov 24, 2012 #2

    lewando

    User Avatar
    Gold Member

    Because your result is insanely large? What were you expecting? No way to help unless you share what you have done.
     
  4. Nov 24, 2012 #3

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    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.
     
  5. Nov 25, 2012 #4
    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: Nov 25, 2012
  6. Nov 25, 2012 #5

    lewando

    User Avatar
    Gold Member

    Try using a smaller step size, like h=.001--to compare this with your manual solution, what did you get?
     
  7. Nov 25, 2012 #6

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    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 (Text):
    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: Nov 25, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook