Convert function handle to a function in ODE45

2 views (last 30 days)
Suppose I have a symbolic matrix J
J = [2*y(1), 4*y(2), y(1)*y(2);
y(3), 2*y(1)^2, 5*y(2);
5*y(3), 2*y(2)*y(1), 2*y(2)];
I can convert it to a function handle using
Jnew = matlabFunction(J)
So now it shows as Jnew = @(y(1),y(2),...)reshape([.....],[3,3]
The issue begins now. Since I want this J to be used in an ode45 environment. For example
[T,Y] = ode45(@(t,y) myode(t,y,Jnew), span, IC)
where myode is
function dy = myode(t,y,J)
vars = y(1:3)
A = [1 2 3;
1 3 4;
7 6 5];
dy = A*vars + J*vars;
end
How can I use J as an input here?

Answers (1)

Walter Roberson
Walter Roberson on 3 Dec 2022
Do not do that. Instead use
A = [1 2 3;
1 3 4;
7 6 5];
Jnew = matlabFunction(A*y(:) + J*y(:), 'vars', {y(:)});
[T,Y] = ode45(Jnew, span, IC)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!