sub plotting odes15 in MATLAB

I am trying to practice for a final coming up and I was given an old question and I am having trouble with the odes15 and sub plotting. I tried using the examples given on the MATLAB main website but couldn't figure out why it would work. The question is:
My code is:
function dx = problem1(t,x)
P1 = 0.028735 ;
P2 = 0.028344 ;
P3 = 5.035 * 10^(-5) ;
Vi = 12 ;
n = 5/54 ;
D_t = 3*exp(-0.05*t) ;
U_t = 3 ;
Gb = 4.5;
Xb = 15;
Ib = 15;
G = x(1);
X = x(2);
I = x(3);
dx = zeros(3,1);
dx(1) = -P1*(G-Gb) - (X-Xb)*G + D_t ;
dx(2) = -P2*(X-Xb) + P3*(I-Ib) ;
dx(3) = -n*I + U_t/Vi ;

Answers (1)

You have done most of it already. Now to call ode15s, you should do like this :
[T,X] = ode15s(@problem1,[0 60*24],[4.5 15 15])
Read more of using ode15s at - http://www.mathworks.com/help/matlab/ref/ode15s.html 60*24 is for 1 day as all the units are in mins. T is time (in mins) and X will be corresponding values at that time where the columns will represent [G I X].
not for subplot:
subplot(3,1,1);
plot(T,X(:,1)); % Plot G
subplot(3,1,2); % Second subplot
plot(T,X(:,2)); % Plot I
subplot(3,1,3); % Second subplot
plot(T,X(:,3)); % Plot X

9 Comments

Thank you, I thought it would be like that, and I looked into that page already put for some reason my command window is printing out that there isn't enought input arguments
Can you post here what you typed and then what error came up!
So I posted this exactly and in the command window I inserted "Problem1" and the command window printed "Not enough input arguments". Thanks for the help:
function dx = problem1(t,x)
P1 = 0.028735 ;
P2 = 0.028344 ;
P3 = 5.035 * 10^(-5) ;
Vi = 12 ;
n = 5/54 ;
D_t = 3*exp(-0.05*t) ;
U_t = 3 ;
Gb = 4.5;
Xb = 15;
Ib = 15;
G = x(1);
X = x(2);
I = x(3);
dx = zeros(3,1);
dx(1) = -P1*(G-Gb) - (X-Xb)*G + D_t ;
dx(2) = -P2*(X-Xb) + P3*(I-Ib) ;
dx(3) = -n*I + U_t/Vi ;
[T,X] = ode15s(@problem1,[0 60*24],[4.5 15 15]) ;
subplot(3,1,1);
plot(T,X(:,1)); % Plot G
subplot(3,1,2); % Second subplot
plot(T,X(:,2)); % Plot I
subplot(3,1,3); % Second subplot
plot(T,X(:,3)); % Plot X
I’m not certain I’m following everything, but you have to put these lines in your main (calling) script:
[T,X] = ode15s(@problem1,[0 60*24],[4.5 15 15]) ;
subplot(3,1,1);
plot(T,X(:,1)); % Plot G
subplot(3,1,2); % Second subplot
plot(T,X(:,2)); % Plot I
subplot(3,1,3); % Second subplot
plot(T,X(:,3)); % Plot X
and not in your ODE function (the rest of the code in your last Comment). From your code, it’s not clear where they are in your code.
If it isn't too much, can you show me, I'm not exactly sure what you mean, but if you meant I am missing things in my code, everything I need for the code is in the pictures above. Thanks for the help
Your ODE function should remain as it is in your initial Question. Don’t add anything to it.
Your main script (that calls ‘problem1’ in ode15s) should have everything in my last Comment. (None of that code should not be in your ODE function, since it leads to recursion problems.)
well, this is what you should do -
Save this as a separate file (a function)
function dx = problem1(t,x)
P1 = 0.028735 ;
P2 = 0.028344 ;
P3 = 5.035 * 10^(-5) ;
Vi = 12 ;
n = 5/54 ;
D_t = 3*exp(-0.05*t) ;
U_t = 3 ;
Gb = 4.5;
Xb = 15;
Ib = 15
G = x(1);
X = x(2);
I = x(3);
dx = zeros(3,1);
dx(1) = -P1*(G-Gb) - (X-Xb)*G + D_t ;
dx(2) = -P2*(X-Xb) + P3*(I-Ib) ;
dx(3) = -n*I + U_t/Vi ;
Once you have saved this, then you can call the rest from command line. Hope this helps !
Just to make it clear, is it possible to put the subplots somewhere within a function whether with this one or separate so that I don't have to manually type in the command window what to plot?? Thanks for the help!!
That is what the script file is for. A script file is what you would otherwise type into the Command Window, but exists as an executable file that you can run and edit anytime. If you make any changes to it, you have to save it again after the changes for them to be executed in the file, since the file executes as the latest saved version.
See the documentation for Create Scripts and Scripts vs. Functions for details. To start the Editor, click on ‘New Script’ at the left under the ‘Home’ tab.
In addition to using the ‘Run’ green triangle, you can execute a particular script by typing its name in the Command Window (with or without the .m extension). That’s what I do (without the extension). The only requirement is your script .m-file has to be in your MATLAB search path.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 11 Dec 2014

Commented:

on 12 Dec 2014

Community Treasure Hunt

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

Start Hunting!