Computing the jacobian of an anonymous function - MATLAB

10 ビュー (過去 30 日間)
Ali Raza
Ali Raza 2022 年 3 月 3 日
編集済み: UserCJ 2022 年 3 月 5 日
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.
  3 件のコメント
Torsten
Torsten 2022 年 3 月 3 日
So somebody stole your question or is your pseudonym "Ali Raza" ?
UserCJ
UserCJ 2022 年 3 月 4 日
I don't use a pseudonym btw!

サインインしてコメントする。

回答 (2 件)

Torsten
Torsten 2022 年 3 月 3 日
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 件のコメント
Torsten
Torsten 2022 年 3 月 4 日
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 2022 年 3 月 5 日
編集済み: UserCJ 2022 年 3 月 5 日
Thank you very much Torsten! Your comments helped me to understand the whole situation.

サインインしてコメントする。


Matt J
Matt J 2022 年 3 月 3 日
編集済み: Matt J 2022 年 3 月 3 日
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,

カテゴリ

Help Center および File ExchangeNumeric Solvers についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by