Errors when using the function ODE 45

Hello! Please help me when using ODE 45! I receive errors:
1) Not enough input arguments. Error in peFun (line 5) eta=ALPHA(1);
2) Error in odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
3) Undefined function or variable 'vPUR'
function fval=peFun(t,ALPHA)
tspan=[0 1];
% Get variables
eta=ALPHA(1);
r=ALPHA(2);
beta=ALPHA(3);
alpha=ALPHA(4);
xP=ALPHA(5);
yP=ALPHA(6);
xE=ALPHA(7);
yE=ALPHA(8);
% Constants
vER=3;
vPUR=5;
a=1;
% Describing dALPHA/dt
fval=zeros(8,1);
fval(1,1)=(-vPUR*SIN(beta)+vER*SIN(alpha))/r;
fval(2,1)=vER*COS(alpha)-vPUR*COS(beta);
fval(3,1)=(a/r)*(vER*SIN(alpha)-vPUR*SIN(beta));
fval(4,1)=(-vER*SIN(alpha)+vPER*SIN(beta))/r;
fval(5,1)=vPUR*COS(eta+beta);
fval(6,1)=vPUR*SIN(eta+beta);
fval(7,1)=vER*COS(eta+alpha);
fval(8,1)=vER*SIN(eta+alpha);
% To run P-E system
%%Initialization
eta0=pi/4;
r0=sqrt(2);
beta0=0;
alpha0=pi/4;
xP0=0;
yP0=0;
xE0=1;
yE0=1;
ALPHA0=[eta0;r0;beta0;alpha0;xP0;yP0;xE0;yE0];
%Solve using ODE45
[tSol,ALPHASol]=ode45(@peFun,tspan,ALPHA0);
%Plot the results
plot(tSol,ALPHASol);
Thank you!

 Accepted Answer

madhan ravi
madhan ravi on 11 Nov 2018
Edited: madhan ravi on 11 Nov 2018
tspan=[0 1];
% To run P-E system
%%Initialization
eta0=pi/4;
r0=sqrt(2);
beta0=0;
alpha0=pi/4;
xP0=0;
yP0=0;
xE0=1;
yE0=1;
ALPHA0=[eta0;r0;beta0;alpha0;xP0;yP0;xE0;yE0];
[tSol,ALPHASol]=ode45(@peFun,tspan,ALPHA0); %function calling before function definition
plot(tSol,ALPHASol);
function fval=peFun(t,ALPHA)
% Get variables
eta=ALPHA(1);
r=ALPHA(2);
beta=ALPHA(3);
alpha=ALPHA(4);
xP=ALPHA(5);
yP=ALPHA(6);
xE=ALPHA(7);
yE=ALPHA(8);
% Constants
vER=3;
vPUR=5;
a=1;
% Describing dALPHA/dt
fval=zeros(8,1);
fval(1,1)=(-vPUR*sin(beta)+vER*sin(alpha))/r;
fval(2,1)=vER*cos(alpha)-vPUR*cos(beta);
fval(3,1)=(a/r)*(vER*sin(alpha)-vPUR*sin(beta));
fval(4,1)=(-vER*sin(alpha)+vER*sin(beta))/r;
fval(5,1)=vPUR*cos(eta+beta);
fval(6,1)=vPUR*sin(eta+beta);
fval(7,1)=vER*cos(eta+alpha);
fval(8,1)=vER*sin(eta+alpha);
end

27 Comments

I got another problem: >> peFun Not enough input arguments.
Error in peFun (line 3) eta=ALPHA(1);
I will be very grateful if you kindly help me!
madhan ravi
madhan ravi on 11 Nov 2018
Edited: madhan ravi on 11 Nov 2018
I got the graph did you try my answer? see the attached screenshot of the graph in my answer
Yes, and received such an error...
Elina Makieva
Elina Makieva on 11 Nov 2018
Edited: madhan ravi on 11 Nov 2018
This is what I insert
madhan ravi's reply : the second picture in this comment is the file you should run by clicking the green button .
madhan ravi
madhan ravi on 11 Nov 2018
Edited: madhan ravi on 11 Nov 2018
Just copy my answer and save it in a script file and click green button
Sorry for stupid questions, I`ve just started learning MATLAB. should I insert your answer in two different windows as it was before or how?
just copy my answer from tspan=[0 1]; till end and paste it in command window and press enter
madhan ravi
madhan ravi on 11 Nov 2018
Edited: madhan ravi on 11 Nov 2018
paste the code here what you tried ,and delete the two files which you saved before
tspan=[0 1];
% To run P-E system
%%Initialization
eta0=pi/4;
r0=sqrt(2);
beta0=0;
alpha0=pi/4;
xP0=0;
yP0=0;
xE0=1;
yE0=1;
ALPHA0=[eta0;r0;beta0;alpha0;xP0;yP0;xE0;yE0];
[tSol,ALPHASol]=ode45(@peFun,tspan,ALPHA0); %function calling before function definition
plot(tSol,ALPHASol);
function fval=peFun(t,ALPHA)
% Get variables
eta=ALPHA(1);
r=ALPHA(2);
beta=ALPHA(3);
alpha=ALPHA(4);
xP=ALPHA(5);
yP=ALPHA(6);
xE=ALPHA(7);
yE=ALPHA(8);
% Constants
vER=3;
vPUR=5;
a=1;
% Describing dALPHA/dt
fval=zeros(8,1);
fval(1,1)=(-vPUR*sin(beta)+vER*sin(alpha))/r;
fval(2,1)=vER*cos(alpha)-vPUR*cos(beta);
fval(3,1)=(a/r)*(vER*sin(alpha)-vPUR*sin(beta));
fval(4,1)=(-vER*sin(alpha)+vER*sin(beta))/r;
fval(5,1)=vPUR*cos(eta+beta);
fval(6,1)=vPUR*sin(eta+beta);
fval(7,1)=vER*cos(eta+alpha);
fval(8,1)=vER*sin(eta+alpha);
end
Elina Makieva
Elina Makieva on 11 Nov 2018
Edited: madhan ravi on 11 Nov 2018
oh,sorry
tspan=[0 1];
% To run P-E system
%%Initialization
eta0=pi/4;
r0=sqrt(2);
beta0=0;
alpha0=pi/4;
xP0=0;
yP0=0;
xE0=1;
yE0=1;
ALPHA0=[eta0;r0;beta0;alpha0;xP0;yP0;xE0;yE0];
[tSol,ALPHASol]=ode45(@peFun,tspan,ALPHA0); %function calling before function definition
plot(tSol,ALPHASol);
function fval=peFun(t,ALPHA)
% Get variables
eta=ALPHA(1);
r=ALPHA(2);
beta=ALPHA(3);
alpha=ALPHA(4);
xP=ALPHA(5);
yP=ALPHA(6);
xE=ALPHA(7);
yE=ALPHA(8);
% Constants
vER=3;
vPUR=5;
a=1;
% Describing dALPHA/dt
fval=zeros(8,1);
fval(1,1)=(-vPUR*sin(beta)+vER*sin(alpha))/r;
fval(2,1)=vER*cos(alpha)-vPUR*cos(beta);
fval(3,1)=(a/r)*(vER*sin(alpha)-vPUR*sin(beta));
fval(4,1)=(-vER*sin(alpha)+vER*sin(beta))/r;
fval(5,1)=vPUR*cos(eta+beta);
fval(6,1)=vPUR*sin(eta+beta);
fval(7,1)=vER*cos(eta+alpha);
fval(8,1)=vER*sin(eta+alpha);
end
Elina Makieva
Elina Makieva on 11 Nov 2018
Edited: madhan ravi on 11 Nov 2018
I can`t insert it in a right formating
madhan ravi's reply: select the whole code and press the code button {} to format your code correctly.
see GIF to understand how it works .
just delete your files peFun.m and runpeFun.m
put clear all at the very beginning and paste the code again in command window and try again
I just copy your answer and paste in command window and press enter because there is not green button when I work with command window
OMG :) I know but if you press enter you should get the result
Ok, just a minute
:))) I deleted all previous files, but... :))
Elina Makieva
Elina Makieva on 11 Nov 2018
Edited: madhan ravi on 11 Nov 2018
OMG!!!IT WORKS!!! :)))) THANK YOU A LOT!!!!!! Could you please tell me, why, when I was doing it in two different windows, I received errors? And what was the initial problem, so I would not repeat it again?
madhan ravi's reply: your initial problem was you did everything correctly but you click run button in the function file(it isn't wrong to do that) but in your case it was because your function required inputs that had to be passed to evaluate those equations.
see common errors while calling a function which will make you understand how the function works.
Elina Makieva
Elina Makieva on 11 Nov 2018
Edited: madhan ravi on 11 Nov 2018
Could you please tell, what function should I insert to receive global phase portrait like that?
madhan ravi's reply : I don't know what a global phase portrait is, that's why I made this comment
when I was doing it in two different windows, I received errors?
function should be put in an mfile with right name in an accessible path or in some recent MATLAB release in the bottom of the script Here is the doc
Thank you @Bruno , @Elina just copy my answer and paste in a new script and save it and make sure to accept the answer since it answered your question
Thank you @Bruno! @Madhan could you please help me with my question about phase portrait? Or I should create new question?
Please create a new question @Elina
Thank you a lot!
madhan ravi
madhan ravi on 11 Nov 2018
Edited: madhan ravi on 11 Nov 2018
Anytime :)
Elina Makieva
Elina Makieva on 11 Nov 2018
Edited: madhan ravi on 11 Nov 2018
I will really appreciate if you could help me with phase portrait https://www.mathworks.com/matlabcentral/answers/429206-errors-when-using-ode-45-phase-portraits

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!