How to access function's (and variable's) value in ODE45 right after each step?

In summary, the "OutputFcn" option in ODE45 allows you to specify a function that will be called after each integration step. This function can access the function's value and variable's value at that step. You can use this option to access the function's and variable's values after each step and use them for further calculations in your code.
  • #1
Ashkan
4
0
first I was thinking it was very easy to obtain a variable's or function's value after each step within ode45 subfunction. But just yesterday I found out all I had done before might be incorrect! just see the MATLAB code below:

I believed the two plots should be exactly the same but they are not! I am very disappointed and confused! can someone explain why they are not the same? and if t2 and F2 are not the actual values after each step, how can I obtain actual values of variable (t) and function (F) after each step?

many thanks in advance,
Ashkan


function tdelay
clear
clc
global Fd ts i F2 t2
Fd=8;
F0=5;
ts=0.01;
i=1;

% F=(F0-Fd)*exp(-t./ts)+Fd;

[t,F]=ode45(@werely,[0 1],F0);

plot(t,F)
hold on
plot(t2,F2,'r')
end

function dF=werely(t,F)
global ts Fd i t2 F2
t2(i)=t;
F2(i)=F;
dF=(1/ts)*(-F+Fd);
i=i+1;
end
 
Physics news on Phys.org
  • #2


Dear Ashkan,

Thank you for sharing your code and concerns with us. After reviewing your code, I believe I have identified the issue that is causing the discrepancy between your two plots.

In your main function, tdelay, you are calling the ode45 function to solve for the values of F at each time step. This function uses a numerical method to approximate the solution at each time step, which means that the values for t and F that are outputted by ode45 are not the exact values at each time step, but rather an approximation.

On the other hand, in your subfunction werely, you are manually calculating the values for t2 and F2 at each time step using a mathematical formula. These values are the exact values at each time step.

So, the reason why your two plots are not identical is because they are showing different values for t and F at each time step. The plot generated by ode45 is showing the approximate values, while the plot generated by your subfunction is showing the exact values.

If you would like to obtain the exact values of t and F after each step, you can do so by modifying your code to save the values outputted by ode45 at each time step, instead of just the final values. You can do this by creating two arrays, t1 and F1, and assigning the output of ode45 to these arrays at each time step. Then, you can plot these arrays instead of t and F, and you should see that they match the plot generated by your subfunction.

I hope this helps to clarify the issue for you. If you have any further questions or concerns, please don't hesitate to reach out.
 

Related to How to access function's (and variable's) value in ODE45 right after each step?

1. Can I access the function's value in ODE45 after each step?

Yes, you can access the function's value in ODE45 after each step by using the "OutputFcn" option. This allows you to specify a function that will be called after each integration step and can access the function's value.

2. How do I access the variable's value in ODE45 after each step?

Similar to accessing the function's value, you can use the "OutputFcn" option to specify a function that can access the variable's value after each integration step.

3. What is the "OutputFcn" option in ODE45?

The "OutputFcn" option in ODE45 allows you to specify a function that will be called after each integration step. This function can access the function's value and variable's value at that step.

4. Can I access the function's value and variable's value simultaneously in ODE45 after each step?

Yes, you can access both the function's value and variable's value simultaneously in ODE45 after each step by using the "OutputFcn" option and specifying a function that can access both values.

5. How can I use the function's and variable's values in ODE45 for further calculations?

You can use the "OutputFcn" option to specify a function that can access the function's and variable's values after each step and store them in variables. These values can then be used for further calculations within the function or in other parts of your code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
870
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
951
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
282
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top