Can some share command for plotting contour plot in MATLAB using BVP4C. I will be extremely grateful.
6 views (last 30 days)
Show older comments
i want plot stream lines graph in matlab using bvp4v code. Can some share command for plotting contour plot in MATLAB using BVP4C. I will be extremely grateful.
1 Comment
Torsten
on 11 Dec 2023
Please share your code. bvp4c has one independent variable, not two. So I don't know how a contour plot (that needs two independent variables) comes into play here.
Answers (1)
Drishti
on 23 Sep 2024
Hi Ali,
The ‘bvp4c’ function is utilized to solve boundary value problems - fourth order method. To achieve a contour plot using ‘bvp4c’ function, we need to first solve the required differential equations.
The solution obtained from ‘bvp4c’ function can be further utilized to get contour plots as ‘contour’ function works on matrix data.
You can refer to the following example to understand the working of ‘bvp4c’ function.
% Define the system of ODEs
function dydx = odefun(x, y)
dydx = [y(2); -y(1)];
end
% Define the boundary conditions
function res = bcfun(ya, yb)
res = [ya(1); yb(1) - 1];
end
% Initial guess for the solution
xmesh = linspace(0, 1, 10);
solinit = bvpinit(xmesh, [0, 1]);
% Solve the BVP
sol = bvp4c(@odefun, @bcfun, solinit);
Furthermore, you can utilize the below code snippet to extend the output value of ‘bvp4c’ function to get contour plots.
% Evaluate the solution on a finer mesh
x = linspace(0, 1, 100);
y = deval(sol, x);
% Create a meshgrid for contour plotting
[X, Y] = meshgrid(linspace(0, 1, 100), linspace(-1, 1, 100));
% Interpolate the solution to get a matrix for contour plot
Z = interp1(x, y(1, :), X);
% Plot the contour using the interpolated solution
figure;
contour(X, Y, Z, 20);
For further information, you can refer to the MATLAB Documentation of ‘bvp4c’ and ‘contour’ functions.
- 'bvp4c' function : https://www.mathworks.com/help/releases/R2024a/matlab/ref/bvp4c.html
- 'contour' function : https://www.mathworks.com/help/releases/R2024a/matlab/ref/contour.html
I hope this resolves your query.
0 Comments
See Also
Categories
Find more on Boundary Value Problems 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!