Computing the jacobian of an anonymous function - MATLAB

10 views (last 30 days)
I'm trying to solve a system of non linear odes in Matlab as follows.
editparams %parameters
Tend = 50;
Nt = 100;
% Define RHS functions
RHS = @(t,x) ModelRHS(t,x,param); %anonymous function defining the set of equations
%Execution-----------------------------------------------------------------
x0 =[0.04;0.75;0.85]; %Initial condition
t = linspace(0,Tend,Nt); %TSPAN
[t x] = ode45(RHS, t, x0);
Now, I need to find the steady state of the system and I'm trying to create a function for this. I thought I'd calculate the steady state using the Jacobian. My equations are in an anonymous function which is defined as f in the code below. However, I realised that jacobian does not work for anonymous functions (or may be there is a way to do with anonymous functions) . so I thought I would convert the anonymous function to a symbolic function and try it. But i still have a difficulty in getting that done. So any help is really appreciated!
function SS = SteadyState(param, E)
f = @(t,x)ModelRHS(t,x,param,E); %set of odes
SymbolicSystem = sym(f); %trying to make the anonymous function symbolic
SymbolicJacobian = jacobian(SymbolicSystem',x); %jacobian
Jacob = matlabFunction(SymbolicJacobian,x);
end
Also if there is any other way apart from finding the Jacobian, kindly let me know about hat too.

Answers (2)

Torsten
Torsten on 3 Mar 2022
Why not simply using "fsolve" to calculate steady state ?
For "fsolve" to work, you only have to supply the function ModelRHS - computation of the Jacobian will be done by "fsolve" internally.
  11 Comments
Torsten
Torsten on 4 Mar 2022
But the steady state function is created using fsolve and it needs x0 as an input
No, it does not need your steady-state x0 as input. It needs an arbitrary vector of length 3 as input.
The syntax is the following:
% Calculate steady-state of your model
xfsolve_start = rand(3,1); % create random vector for fsolve to start with
x0 = SteadyState(xfsolve_start,param,E); % calculate steady-state of the model equations
% Prepare time-dependent calculation
Tstart = 0.0;
Tend = 50;
Nt = 100;
% Define RHS functions
RHS = @(t,x) ModelRHS(t,x,param,E); %anonymous function defining the set of equations
%Execution-----------------------------------------------------------------
t = linspace(Tstart,Tend,Nt); %TSPAN
[t ,x] = ode45(RHS, t, x0); % Start your time-dependent calculation in steady-state
function x0 = SteadyState(xfsolve_start,param,E)
t = 0; % Chosen arbitrarily ; doesn't influence steady-state solution since RHS does not explicitly depend on t (I hope)
RHS = @(x)ModelRHS(t,x,param,E);
x0 = fsolve(RHS,xfsolve_start);
end
UserCJ
UserCJ on 5 Mar 2022
Edited: UserCJ on 5 Mar 2022
Thank you very much Torsten! Your comments helped me to understand the whole situation.

Sign in to comment.


Matt J
Matt J on 3 Mar 2022
Edited: Matt J on 3 Mar 2022
On the File Exchange, there are numerical methods for approximating the Jacobian of a non-symbolic function using finite differences , e.g.,
You should probably add a post-processing step that smooths the output of ModelRHS() with a smoothing spline, however. ODE functions seem to have a tendency to produce noisy, non-differentiable output, see,

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!