0
votes
function dx=m1(t,x)
    dx(1)=(l0*x(1))/(1+(l0*x(1))
endfunction 

function dx=m2(t,x)
    dx(1)=(l0*x(1))/[1+(l0*(x(1)+x(2))]
    dx(2)=(k2*x(1)*x(1))-k1*x(2)
endfunction

t0=15;

function f(t,t0)
    if (t < t0)
    {
        return m1(t)
    }
    else
    {
       return m2(t)
    }
    end
endfunction

x=ode(x0, t0, t, f);

In the above code, I am defining two functions m1 and m2 and on the basis of a time point, say, t0, I am returning either of the functions. However, the error is showing undefined variable:x. Is it because I am using x(1) in both the functions? x is basically a vector containing x(1) in the first function m1, and [x(1);x(2)] in m2.

P.S.- all the required value of the constants including the initial values are given.

1

1 Answers

0
votes

You can write the function as follows:

function [dx] = calc_dx( t, x)
    verbose = 1
    k1 = 2
    k2 = 3
    dx = []
    t0 = 15
    if (t < t0)
        dx(1) = ( 10 * x(1) ) / ( 1 + ( 10 * x(1) ) )
    else
        dx(1) = ( 10 * x(1)) / ( 1 + ( 10 * ( x(1) + x(2) ) ) )
        dx(2) = ( k2 * x(1) * x(1) ) - k1 * x(2)
    end
endfunction

I have presumed that k1 and k2 are constants and not other variables and have assigned them values.

Remember to keep your present working directory to where you have kept the '.sci' file for the above function.

From the command window run

exec filename.sci;

to load your function into scilab.