marco
marco

Reputation: 975

Matlab solving ODE applied to State Space System, inputs time dependent

I've got at State System, with "forced" inputs at bounds. My SS equation is: zp = A*z * B. (A is a square matrix, and B colunm)

If B is a step (along the time of experience), there is no problem, because I can use

  tevent = 2;
  tmax= 5*tevent;

  n =100;
  dT = n/tmax;
  t = linspace(0,tmax,n);
  u0 = 1 * ones(size(z'));
  B = zeros(nz,n);
  B(1,1)= utop(1)';
  A = eye(nz,nz);

  [tt,u]=ode23('SS',t,u0);

and SS is:

  function zp = SS(t,z)
          global A B
          zp = A*z + B;
  end

My problem is when I applied a slop, So B will be time dependent.

  utop_init= 20;
  utop_final = 50;
  utop(1)=utop_init;
  utop(tevent * dT)=utop_final;

  for k = 2: tevent*dT -1
      utop(k) = utop(k-1) +(( utop(tevent * dT) - utop(1))/(tevent * dT));
  end

  for k = (tevent * dT) +1 :(tmax*dT)
      utop(k) = utop(k-1);
  end

  global A B
  B = zeros(nz,1);
  B(1,1:n) = utop(:)';
  A = eye(nz,nz);

I wrote a new equation (to trying to solve), the problem, but I can't adjust "time step", and I don't get a u with 22x100 (which is the objective).

  for k = 2 : n
  u=solveSS(t,k,u0);
  end

SolveSS has the code:

function [ u ] = solveSS( t,k,u0)

  tspan = [t(k-1) t(k)];

  [t,u] = ode15s(@SS,tspan,u0);

      function zp = SS(t,z)
          global A B
          zp = A*z + B(:,k-1);
      end

  end

I hope that you can help!

Upvotes: 0

Views: 4242

Answers (1)

foglerit
foglerit

Reputation: 8269

You should define a function B that is continuously varying with t and pass it as a function handle. This way you will allow the ODE solver to adjust time steps efficiently (your use of ode15s, a stiff ODE solver, suggests that variable time stepping is even more crucial)

The form of your code will be something like this:

function [ u ] = solveSS( t,k,u0)

    tspan = [t(k-1) t(k)];

    [t,u] = ode15s(@SS,tspan,u0,@B);

        function y = B(x)
            %% insert B calculation
        end

        function zp = SS(t,z,B)
            global A
            zp = A*z + B(t);
        end

    end

Upvotes: 2

Related Questions