Have the user input a filename containing an ODE, which has to be passed to a function.

1 view (last 30 days)
Im writing a custom phase portraint function which requires the corresponding ODE. The ODE is written as a function in a separate file. I want the user to input the filename/function name of the ODE by typing this in the main script. This file name should then be passed to the Phase portrait function such that it can use the ODE function. How to do this in such a way that the user only has to enter this once?
This the main code which calls the Phaseportrait function to draw the phase plot of the corresponding ODE:
PlotSize = [-2, 8, 30, -5, 5, 30]; %Size and resolution of the plot [x1_1 x1_2 x1_res x2_1 x2_2 x2_res]
Vscale = 0.7; %Scale factor of vectors. 1 is no scaling.
AutoScale = 0; %Scale vectors depending on velocity [on=1, off=0]
Modelname = 'righthandside';
PhasePortrait(@Modelname, PlotSize, Vscale, AutoScale)
The ODE in a seperate file:
function dxdt = righthandside(t,x)
dxdt(1) = x(2);
dxdt(2) = -sin(x(1));
end
And the code in the custom phase portrait function which requires the ODE:
function PhasePortrait(MODEL, PlotSize, Vscale, AutoScale)
% (Some irrelevent code has been left out)
Xprime = MODEL(t,[x1_mesh(i); x2_mesh(i)]);

Answers (1)

Cris LaPierre
Cris LaPierre on 28 Sep 2022
The problem is that (t,[x1_mesh(i); x2_mesh(i)]) is being treated as indexing your variable MODE. It is not being recognized as a function. I think you want to use feval here. Also, remove the '@' before Modelname in the input to PhasePortrait.
PlotSize = [-2, 8, 30, -5, 5, 30]; %Size and resolution of the plot [x1_1 x1_2 x1_res x2_1 x2_2 x2_res]
Vscale = 0.7; %Scale factor of vectors. 1 is no scaling.
AutoScale = 0; %Scale vectors depending on velocity [on=1, off=0]
Modelname = 'righthandside';
PhasePortrait(Modelname, PlotSize, Vscale, AutoScale)
Xprime = 1×2
15.0000 0.9589
function dxdt = righthandside(t,x)
dxdt(1) = x(2);
dxdt(2) = -sin(x(1));
end
function PhasePortrait(MODEL, PlotSize, Vscale, AutoScale)
% (Some irrelevent code has been left out)
% ## I had to define some varialbes so the code would run
t = [0 10];
i=1;
x1_mesh(1) = 5;
x2_mesh(1) = 15;
Xprime = feval(MODEL,t,[x1_mesh(i); x2_mesh(i)])
end

Community Treasure Hunt

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

Start Hunting!