Help understanding ODE code
2 views (last 30 days)
Show older comments
I found this code online, and I'm trying to understand exactly how it works. All I've really been able to understand is that f2zero finds f"(0) with a guess of '1'. the function odesol and the input f2zeroiter is the part I can't really understand.
Here's the code:
function f2zero = boundarylayerblasius dydx = @(t,f) [f(2); f(3); -0.5 * f(1) * f(3)]; f2zeroguess = 1; function f1infm1 = odesol(f2zeroiter) [T,F] = ode45(dydx,[0,5.2],[0,0,f2zeroiter]); f1infm1 = F(end,2)-1; end f2zero = fzero(@odesol, f2zeroguess); plot(T,F, 'linewidth',2); legend('f','f''','f"', 'location', 'northwest') end
0 Comments
Answers (1)
Hari
on 6 Jan 2025
Hi Josh,
I understand that you are trying to comprehend a MATLAB code that solves a boundary layer problem using the Blasius equation. You are specifically confused about the "odesol" function and its interaction with "f2zero".
Defining the ODE System:
The function "dydx" defines the system of ordinary differential equations (ODEs) for the Blasius boundary layer problem. It is a system of three first-order ODEs.
dydx = @(t,f) [f(2); f(3); -0.5 * f(1) * f(3)];
Initial Guess for f''(0):
"f2zeroguess" is the initial guess for the unknown boundary condition f''(0). This is a common approach in shooting methods for boundary value problems.
f2zeroguess = 1;
ODE Solution Function (odesol):
The function "odesol" solves the ODEs using ode45 with the guessed value of f''(0). It calculates the solution over the interval [0, 5.2] with initial conditions [0, 0, f2zeroiter].
The purpose of "odesol" is to return the difference between the computed and desired boundary condition at infinity (f'(infinity) = 1).
function f1infm1 = odesol(f2zeroiter)
[T,F] = ode45(dydx,[0,5.2],[0,0,f2zeroiter]);
f1infm1 = F(end,2) - 1;
end
Finding the Correct f''(0) with fzero:
fzero is used to find the root of odesol, i.e., the value of f''(0) that makes f'(infinity) = 1. It iteratively adjusts f2zeroiter until the condition is satisfied.
f2zero = fzero(@odesol, f2zeroguess);
Plotting the Solution:
The solution is plotted to visualize the functions f, f', and f''.
plot(T,F, 'linewidth',2);
legend('f','f''','f"', 'location', 'northwest');
Refer to the documentation of "ode45" for more details: https://www.mathworks.com/help/matlab/ref/ode45.html
Refer to the documentation of "fzero" for more details: https://www.mathworks.com/help/matlab/ref/fzero.html
Hope this helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!