Computing the jacobian of an anonymous function - MATLAB
조회 수: 10 (최근 30일)
이전 댓글 표시
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
답변 (2개)
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
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
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.,
https://www.mathworks.com/matlabcentral/fileexchange/13490-adaptive-robust-numerical-differentiation
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,
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!