A Lorenz's system of ODEs doesn't get executed in Matlab

In summary: A-K-s5I[/youtube]In summary, I used the following script and function in MatLab, but get three errors.
  • #1
MathematicalPhysicist
Gold Member
4,699
371
I use the following script and function in MatLab, but get three errors.
I shall first write down the code and after that the errors that I get.

Matlab:
function yprime = lorenz_de(t,y)
%LORENZ_DE    Lorenz equations.
%   yprime  = lorenz_de(t,y).

yprime = [10*(y(2)-y(1))
          28*y(1)-y(2)-y(1)*y(3)
          y(1)*y(2)-8*y(3)/3];

Matlab:
%LORENZ_RUN     ODE solving example: Lorenz.

tspan = [0 50];                       % Solve for 0 <= t <= 50.
yzero = [0;1;0];                      % Initial conditions.
[t,y] = ode45(@lorenz_de,tspan,yzero);
plot(y(:,1),y(:,3))                   % (y_1,y_3) phase plane.
xlabel('y_1','FontSize',14)
ylabel('y_3 ','FontSize',14,'Rotation',0,'HorizontalAlignment','right')
title('Lorenz equations','FontSize',16,'FontWeight','normal')

Here are the errors:
Code:
Error using odearguments (line 93)
LORENZ_DE must return a column vector.

Error in ode45 (line 115)
  odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);

Error in lorenz_run (line 5)
[t,y] = ode45(@lorenz_de,tspan,yzero);

How to fix these errors?
Thanks!
 
Physics news on Phys.org
  • #2
Works perfectly fine for me:
1587099581273.png
 
  • #3
@Orodruin thanks for replying.

It seems something is wrong with my software, can you give a look at the errors I listed in my OP?
BTW which version of MatLab are you using?

I am using 2018b, and now I am downloading update 2.
 
  • #4
I have a rather old version of Matlab, 2014b if I recall correctly ...

The errors suggest that your lorenz_de function does not return a column vector, but it does. The only thing I can think about is if your main file and lorenz_de are located in different folders and you have an old copy of lorenz_de lying around in the same folder as the main file.
 
  • Like
Likes MathematicalPhysicist and Wrichik Basu
  • #5
@MathematicalPhysicist Your code works fine in my 2019b.

One thing that you can try is to convert the function into an inner function in the script. This way, the script will only refer to the function within the script and you can avoid the the duplicate files issue stated by @Orodruin in #4.

Another option is to completely convert the script into a function. Let the inner (nested) function remain as it is. This way, there will be no collision with workspace variables, if any.
 
Last edited:
  • Like
Likes MathematicalPhysicist
  • #6
It works!

Sing Halleluja!
It seems I should have saved the two files in the directory of MatLab and then make the directory into the add to path.
[youtube]
 
  • Like
Likes Wrichik Basu

Related to A Lorenz's system of ODEs doesn't get executed in Matlab

1. Why is my Lorenz system of ODEs not executing in Matlab?

There could be several reasons for this. One possibility is that there is an error in your code, such as a missing or incorrect variable declaration. Another possibility is that your initial conditions are not set properly, causing the system to fail to converge. It could also be an issue with your Matlab software itself, in which case you may need to troubleshoot or reinstall it.

2. How can I debug my Lorenz system of ODEs in Matlab?

To debug your code, you can use the "debug" mode in Matlab. This allows you to step through your code line by line and see where any errors may be occurring. You can also use the "disp" function to print out specific variables at different points in your code to check their values and make sure they are correct.

3. Are there any common mistakes to watch out for when implementing a Lorenz system of ODEs in Matlab?

One common mistake is not properly defining or updating your initial conditions. Another mistake is not using the correct syntax for the ODE solver function, such as not specifying the correct variables or not setting appropriate options. It is also important to double check any initial parameters or equations to make sure they are correct.

4. Can I use Matlab to solve a Lorenz system of ODEs with symbolic variables?

Yes, Matlab has a symbolic math toolbox that allows you to work with symbolic variables and equations. You can use this toolbox to solve your Lorenz system of ODEs with symbolic variables, and then convert the results back to numerical values if needed.

5. How can I improve the efficiency of my Lorenz system of ODEs in Matlab?

One way to improve efficiency is to vectorize your code, which means using arrays and matrix operations instead of individual variables. This can greatly speed up the execution of your code. You can also try using a different ODE solver function or adjusting the options for the solver to see if it improves performance.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
847
  • Advanced Physics Homework Help
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
7K
Back
Top