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 CenterFile Exchange에서 Numeric Solvers에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by