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

  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    Matlab Odes System
Click For Summary
The discussion revolves around troubleshooting errors encountered while executing a Lorenz system of ODEs in MATLAB. Users report receiving errors indicating that the function must return a column vector, despite the code appearing correct. Suggestions include checking for duplicate function files in different directories and converting the function into an inner function within the script to avoid conflicts. One user successfully resolved the issue by ensuring both files were saved in the correct MATLAB directory and adding that directory to the path. The conversation highlights the importance of file management and function scope in MATLAB programming.
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 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 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 Wrichik Basu

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · 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 ·
Replies
2
Views
7K
Replies
2
Views
2K