Solving a system of differential equation
Show older comments
Can someone tell me why the answers are coming as 'NaN'?
Also in the first for loop, by writing dEs in descritized form (line 22 and 23) my aim is to find (dEs/dt) and use the value of Es in line 23. Also,after this loop, the final values of El and Es needs to be carried over to next loop. I have coded this as best as i know. Can someone tell me how i can find d(Es)/dt directly instead of discretizing it?
7 Comments
Ameer Hamza
on 19 Oct 2020
Can you show your ODEs? Also, in hemicellulose.m, you need to output the value of the derivative (dEs), not the output of the function (Es).
Abhishek Varma
on 19 Oct 2020
Abhishek Varma
on 19 Oct 2020
Abhishek Varma
on 19 Oct 2020
Ameer Hamza
on 19 Oct 2020
The way you wrote your function seems to be confusing. You are also using time as a state?
f(3) = t;
You also seems to be using something like Euler method in the following line
Es = Es + dEs*(t-t_pre);
Also, it seems that you are using X_next to share infomation between iterations of ode45.
Following shows a simple example of how to write function for an ODE
function dydt = odeFun(t, y)
dydt = -2*y.^2;
end
you don't need to do something like the following
function f = odeFun(t, y)
Y = y(1);
t_pre = y(2);
dydt = -2*y(1).^2;
Y = Y + dydt(t-t_pre);
f(1) = Y;
f(2) = t;
end
Abhishek Varma
on 20 Oct 2020
Ameer Hamza
on 20 Oct 2020
But a scheme like this will not work as expected. ode45() does not do one calculation in each iteration. It is an adaptive step-size algorithm, and the point moves forward and backward several times in each iteration for ode45 to decide what should be a good step length. So You might be expecting that it is the value from the previous iteration, but is not exactly that.
Answers (0)
Categories
Find more on Ordinary Differential Equations 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!
