0

I'm trying to work with a code that involves ode45, in the one in the equation that will be solved I have to introduce a single value from a vector that will change depending on the time the equation is solved, I mean if it is x=x(0) then u1(1) and u2(t), when x=x(1) then u1(2), u2(2)... This is my code:

function [sal] = scobe1
          clear
          clc
  global  a;
           a=1;
          assignin('base','a',a)
          tspan = [0; 180];              
          x0 = [80; 0];
          [t,x] = ode45(@f,tspan,x0);         
          sal=[t,x]
          figure
          subplot(2,1,1)
          plot(t,x(:,1),'k-');
          subplot(2,1,2)
          plot(t,x(:,2),'b-');
   function dxdt = f(t,x)
         global a s1  u1 u2 newData;
newData=importdata('datosnoembarazo.mat');
assignin('base','newData',newData);
u1=getfield(newData,'glucosa4dias');
assignin('base','u1',u1);
u2=getfield(newData,'insulina4dias');
assignin('base','u2',u2);
u1=getfield(newData,'glucosa4dias');
assignin('base','u1',u1);
u2=getfield(newData,'insulina4dias');
assignin('base','u2',u2);
            if (a<=s1) 
             dxdt = [    
                (-4.9e-2-x(2))*x(1) + (4.42 + u1(a)) %(1)  IN HERE u1 IS THE VECTOR'S NAME
                -9.1e-2*x(2) + u2(a) %(2) IN HERE u2 IS THE VECTOR'S NAME
                ];
             a=a+1
             assignin('base','a',a);
            else return
            end

The problem is that it sends me this error: And I don't know what is wrong with the code, or what else can I do in order for it to read it, can you please help me? Thanks

Error in scobe1>f (line 36)
       global a s1 s2 u1 u2 newData;
Error using feval
Output argument "dxdt" (and maybe others) not assigned during call to
"C:\Users\AnnieA\Dropbox\Tesis (A. Olay)\GUI Examples\Resumen de Modelos\scobe1.m>f".
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 scobe1 (line 25)
        [t,x] = ode45(@f,tspan,x0);
Ana Olay
  • 3
  • 2

1 Answers1

0

Your function function dxdt = f(t,x) has two branches in the if statement: If a<=s1, then dxdt gets assigned correctly. If a>s1, dxdt is not assigned at all. You can fix the error by assigning a value to dxdt in the else branch of your if statment before you return. I am not sure what an appropriate value would be in this case. Perhaps 0?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Do you have any idea of what is wrong with this: http://stackoverflow.com/questions/33987704/how-to-interpolate-a-vector-and-work-with-variables-ode45?noredirect=1#comment55733662_33987704 ? Thanks – Ana Olay Nov 30 '15 at 03:01