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

  • Context: MATLAB 
  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    Matlab Odes System
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting errors encountered while executing a Lorenz system of ordinary differential equations (ODEs) in MATLAB. Participants explore potential causes of the errors and suggest solutions related to software versions and file organization.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • The original poster (OP) reports receiving errors indicating that the function must return a column vector.
  • Some participants, like @Orodruin, suggest that the issue may stem from having multiple copies of the function in different folders.
  • Another participant proposes converting the function into an inner function within the script to avoid file collision issues.
  • Participants mention different versions of MATLAB being used, with some working fine while others encounter errors.
  • The OP later confirms that saving the files in the correct directory resolved the issue.

Areas of Agreement / Disagreement

There is no consensus on the exact cause of the errors, as participants suggest different potential solutions and experiences with varying MATLAB versions. The discussion includes both successful and unsuccessful attempts to resolve the issue.

Contextual Notes

Participants reference specific versions of MATLAB, indicating that software version differences may play a role in the issues encountered. There is also mention of file organization affecting function execution.

Who May Find This Useful

Users experiencing similar issues with MATLAB ODEs, particularly those working with the Lorenz system or troubleshooting function execution errors.

MathematicalPhysicist
Science Advisor
Gold Member
Messages
4,662
Reaction score
372
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
Works perfectly fine for me:
1587099581273.png
 
@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.
 
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   Reactions: MathematicalPhysicist and Wrichik Basu
@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   Reactions: MathematicalPhysicist
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   Reactions: Wrichik Basu

Similar threads

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