Matlab - Weird answer with all zeroes

In summary: This is because the Euler's formula operates on real numbers, and the computer can only store so many at a time. Change your units. ##1/2^6## or so.In summary, the code is trying to solve for x1 and x2 by Euler's forward formula, but is getting nonsense results because the time interval is too large. To solve the system, I need to change the time interval to be smaller.
  • #1
eurekameh
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?
 
Physics news on Phys.org
  • #2
Because your result is insanely large? What were you expecting? No way to help unless you share what you have done.
 
  • #3
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
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
Try using a smaller step size, like h=.001--to compare this with your manual solution, what did you get?
 
  • #6
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:

What is Matlab?

Matlab is a programming language and numerical computing environment used in various scientific and engineering fields to analyze and visualize data, develop algorithms, and create models and simulations.

Why am I getting an answer with all zeroes in Matlab?

This could be due to a variety of reasons, including incorrect input, faulty code, or an error in the logic of your program. It is important to carefully check your code and data inputs to identify the source of the issue.

How can I troubleshoot an issue with all zeroes in Matlab?

One way to troubleshoot this issue is to use the debugging tools in Matlab to step through your code and track the values of your variables. This can help you identify where the problem is occurring and make necessary adjustments.

Can Matlab handle large datasets?

Yes, Matlab has the ability to handle large datasets through its efficient and powerful matrix-based operations. However, it is important to optimize your code and use memory management techniques to ensure efficient processing of large datasets.

Is Matlab only used for scientific research?

No, Matlab has a wide range of applications and is used in various industries, including finance, data analysis, image and signal processing, and more. It is a versatile tool that can be applied in many fields for various purposes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
944
  • Engineering and Comp Sci Homework Help
Replies
1
Views
939
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Introductory Physics Homework Help
Replies
7
Views
761
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
Back
Top