How to use substitution on function handle?
4 views (last 30 days)
Show older comments
I use an example to simplify my problem. Some steps are as follows:
(1) There is a system of linear equations such as
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550502/image.png)
Use analytical method to solve
and
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550507/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550512/image.png)
(2) And a differential equation is
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550517/image.png)
Substitute
and
solved from step(1) into the differential equation, then use numerical method to solve
with
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550507/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550512/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550532/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550537/image.png)
(3) Another differential equation is
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550542/image.png)
Replace A with
solved from step(2), then use numerical method to solve
with
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550532/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550552/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550567/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1550532/image.png)
clear
clc
% Step 1
syms z x y;
A = [5 2; 1 1];
B = [12*z; 3*z];
Sol = A\B;
xz = matlabFunction(Sol(1));
yz = matlabFunction(Sol(2));
% Step 2
syms a;
ode_a = @(z, a) xz(z)*yz(z)*a;
initial_a = 1;
[z_a, values_a] = ode45(@(z, a) ode_a(z, a), [0, 1], initial_a);
a_sol = @(z) interp1(z_a, values_a, z, 'spline'); % Here a_sol is a function handle.
% Step 3
syms A
ode_b = @(z, b) 6*A;
ode = subs(ode_b, A, a_sol(z)); % This step mistakes, "subs" can not be used for function handle.
initial_b = 0;
[z_b, b_values] = ode45(@(z, b) ode(z, b), [0, 1], initial_b);
0 Comments
Answers (1)
John D'Errico
on 23 Nov 2023
Edited: John D'Errico
on 23 Nov 2023
ODE45 CANNOT use symbolic parameters. PERIOD. No matter how you try to trick things, you cannot pass a symbolic parameter (here A) into ODE45. For that matter, neither can you do that with INTERP1.
INTERP1 and ODE45 are purely numerical tools.
If you know the value of that parameter, then yes, you can do what you ask. But then it becomes a purely numerical problem.
1 Comment
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!