-2
$\begingroup$

After using ode45 to solve a set of ODEs, I want to write a Matlab function to take the initial conditions x_0 as inputs and gives the final state x_1 at time T as the output, thus creating a discrete dynamical system f.

However, I'm having some trouble writing the function that would embed the initial conditions, the ode45 solver, and the final state as the output.

My attempt:

% F is the function handle to the set of ODEs to be solved

        x_0 = some initial conditions here
        
        T = 10 
        
            function x_1 = f(x_0);
                
            [t, y] = ode45( F, [0 T], x_0 );
            
    % store final state values in variables 
    
            x_final     = y( end, 4 );
            y_final     = y( end, 5 );
            theta_final   = y( end, 3 );
      
    % now define the output of f to take the above final state values
      
             x_1 = [ x_final y_final theta_final ];
            
            end

This doesn't seem to work. I get the messages:

(1) The function 'f' might be unused

(2) The value assigned here to 't' appears to be unused. Consider replacing it with ~.

What am I missing?

Thanks,

$\endgroup$
  • $\begingroup$ The problem is that you are not calling the function $f$ which you have defined. You call it by writing $x_1 = f(x_0)$ without the function keyword. $\endgroup$ – Christoph Aug 31 at 3:08
0
$\begingroup$

You want to solve the initial value problem \begin{equation} \dot{x} = f(t,x), \quad x(0) = x_0, \end{equation} and then to evaluate the function $F : x_0 \mapsto y = x(T)$, where $T > 0$ denotes the final time. In MATLAB this can be done with a nested function, for example:

function y = F(x0)
    T = ...
    [t,x] = ode45(@f,[0 T],x0);
    y = x(end,:);

    function xp = f(t,x)
       xp = ...
    end
end

Once defined you can call the function $F$ in a MATLAB script with specific initial values $x_0$ to obtain the corresponding final values $y$.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ the function $f(t,x)$ should evaluate the right-hand side of your ODE system. The function $F$ is called with an input (initial value) $x_0$ and it outputs the final value $y = x(T;x_0)$. You can use the function $F$ inside a script, for example, where you can store the pairs $(x_0,y)$. $\endgroup$ – Christoph Aug 30 at 11:24
  • $\begingroup$ Yes, the code fragment that I gave should be stored in a separate file F.m. Then the function F can be called inside a script (i. e. a different file, say main.m) which is stored inside the same directory as F.m. $\endgroup$ – Christoph Aug 30 at 11:30
  • $\begingroup$ No, @f is just a handle to a function that needs to be defined somewhere. This happens in the nested function definition. $\endgroup$ – Christoph Aug 30 at 18:24
  • $\begingroup$ Variables used in a MATLAB function are local, so it is normal that you cannot access it from the command window. But if you call the function F as in >> y = F([...]), then $y$ should be defined afterwards. The other errors about F and t I don't understand. We might have to continue the discussion in a chat. $\endgroup$ – Christoph Aug 30 at 18:44
  • $\begingroup$ Hi Christoph, I've posted a code sample of mine - thanks. $\endgroup$ – user36920 Aug 30 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.