TransWikia.com

Writing a Matlab function after calling the ode45 solver

Computational Science Asked by user36920 on March 17, 2021

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,

One Answer

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$.

Answered by Christoph on March 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP