Error in code, trying to get plot

1 view (last 30 days)
Stephen
Stephen on 13 Nov 2015
Answered: Walter Roberson on 13 Nov 2015
Function below
function yprime = meen668_project5_3(t,tm,y)
yprime(1,1)=15-((3*(y(1))*t)/800); %%Equation 1, Example 2 1000 gallon holding tank from Paul's Online Math Notes
yprime(2,1)=(2*(y(2))*t)/(400-(t-tm)); %Equation 2, Example 2 1000 gallon holding tank from Paul's Online Math Notes
Script below
y0(1,1)=2;
y0(2,1)=500;
%Numerical solution
tspan1=[0,35.475];
tspan2=[35.475,435.4758];
[t,tm,y]=ode45('meen668_project5_3',tspan1,tspan2,y0);
%Analytical solution
%Plot
figure(1)
Y1=y(:,1);
Y2=y(:,2);
plot(t,Y1,'b-',t,Y2,'g*')
title('Example 2 1000 gallon holding tank from Paul''s Online Math Notes')
xlabel('Time')
ylabel('Pollution')
legend('Numerical')
Error below
Attempted to access y(1); index out of bounds because numel(y)=0.
Error in meen668_project5_3 (line 3)
yprime(1,1)=15-((3*(y(1))*t)/800); %%Equation 1, Example 2 1000 gallon holding tank from Paul's Online Math Notes
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in figure1_meen668_proj5_3 (line 7)
[t,tm,y]=ode45('meen668_project5_3',tspan1,tspan2,y0);

Answers (1)

Walter Roberson
Walter Roberson on 13 Nov 2015
You have coded
[t,tm,y]=ode45('meen668_project5_3',tspan1,tspan2,y0);
which is an obsolete syntax that is no longer documented, which was officially declared not recommended as of MATLAB 5.1. You need to switch over to anonymous function handles and read http://www.mathworks.com/help/matlab/math/parameterizing-functions.html for how to pass in your extra variable "y"
Your tspan2 variable is being passed in the position that ode45 calls y0, which is the variable that holds the boundary conditions, with the boundary conditions for any one call being received as the second parameter of fun, which you call tm in your code. But you then have a parameter named y0 to your ode45 call, that is being passed in the options parameter position. What ode45 is going to do with that is no longer documented, and as such it is entirely valid now for ode45 to decide that it should mean that an empty vector should be passed as the third argument. It is also entirely valid now for ode45 to decide that passing a parameter there which is not an options structure should indicate that you want ode45 to find the nearest printer and cause the printer to play "Anchors Away" (or, I guess, DOOM)
I would suggest to both you and G (whose question, posted slightly after yours, was identical) that you should not be passing two tspan variables, but that possibly you want a single timespan variable created as [0,35.475,435.4758] . But you would then need to adjust your code to expect two variables instead of three like it has now.
Please remember that the second parameter that is passed in to the ode45 routine might always have the same size as the boundary conditions list, but that ode45 continually calculates which boundary values it wishes to probe in order to find the proper shape of the curve. You can test with ode45 and you will find that the time (first parameter) is sometimes repeated to check different boundary conditions and that the second parameter passed in to one call will not be whatever dy/dx was passed out of the previous call. ode45 has its own way of determining what values to pass for the second parameter. Therefore if you are relying on the second parameter to convey conditions for an externally imposed time-based change of state (e.g., that the behavior of the system is to change from filling to draining at time 35.475) then you need to code that a different way, such as based upon the first parameter (which will be time.)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!