plotting Graph in 3D help
Show older comments
basic()
function basic
K=0.2; n=0.3; lam=0.1;
%Defining parameters
sol1 = bvpinit(linspace(0,6,300),[0 0 0 0 0]);
sol = bvp4c(@bvp2D, @bc2D, sol1);
x = sol.x;
y = sol.y;
%%% Plotting of the velocity
figure (1)
plot(x, y(2, :) ,'linewidth', 1.5);
hold on
xlabel('\eta', 'fontweight', 'bold', 'fontsize', 16)
ylabel('f^{\prime}(\eta)', 'fontweight', 'bold', 'fontsize', 16)
%% Residual of the boundary conditions
function residual = bc2D(y0, yinf)
residual=[y0(1); y0(2) - 1; y0(4)+n*y0(3); yinf(2)-lam; yinf(4)];
end
%% System of First Order ODEs
function yvector = bvp2D(~,y)
yy1 =(1/(1+K))*(-y(1)*y(3)+y(2)^2-K*y(5)-lam^2);
yy2 =(2/(2+K))*(-y(1)*y(5)+y(2)*y(4)+K*(2*y(4)+y(3)));
yvector = [y(2);y(3);yy1; y(5); yy2];
end
end
i want to plot this graph in 3D, how can I?
Accepted Answer
More Answers (1)
Umar
on 7 Jul 2024
0 votes
Hi Fareeha,
In response to your first question, to confirm if you are getting the right figure after using the "z=peak();" function in your code, you can employ various methods. One common approach is to visually inspect the output or result generated by the function to see if it aligns with your expectations. This could involve plotting or displaying the figure to ensure it matches what you anticipate. Furthermore, you can compare the output obtained from "z=peak();" with known values or expected outcomes to validate its accuracy. By verifying against established benchmarks or test cases, you can assess if the figure produced by the function is correct. Also, bear in mind that in MATLAB, the function z=peak(); is typically used to generate a sample data matrix that can be visualized as a contour plot. When you use this function, it creates a 49x49 matrix with peak values that form a peak in the center. If you want to generate the correct contour plot using this data, you do not necessarily have to modify your code. However, it is important to ensure that you are using the correct variable name when accessing the data matrix. In this case, if you initially used z=peak(); to store the data matrix, you should continue to reference it as z in your code.
Regarding your second query about using "Z=peak();" instead of "z=peak();", it typically depends on the programming language or environment you are working in. In some languages, variable names are case-sensitive, meaning that "z" and "Z" would be treated as distinct variables. Therefore, using "Z=peak();" might create a new variable "Z" rather than updating the existing variable "z". To ensure consistency and prevent potential errors, it is advisable to stick to a consistent naming convention for variables within your code. If you intend to update the original variable "z" with a new value from "peak();", it is recommended to use the same case for consistency.
Hope this answers all your questions.
8 Comments
Fareeha
on 26 Jul 2024
Umar
on 27 Jul 2024
Edited: Walter Roberson
on 29 Jul 2024
Hi Fareeha,
To modify the existing code to include streamlines, obtain the solution sol using bvp4c by extracting the velocity components u and v from sol.y to create a meshgrid for streamlines. Here is code snippet to show how to do it,
u = sol.y(3, :); % Extract u component
v = sol.y(2, :); % Extract v component
[X, Y] = meshgrid(x, y(1, :)); % Create meshgrid for streamlines
Next, plot the streamlines using the streamline function. Adjust the density of streamlines by specifying the starting points and the streamline spacing.
figure(2);
streamline(X, Y, reshape(u, size(X)), reshape(v, size(X)), [start_x1, start_x2, ...], [start_y1, start_y2, ...]);
xlabel('x', 'fontweight', 'bold', 'fontsize', 16);
ylabel('y', 'fontweight', 'bold', 'fontsize', 16);
title('Streamlines', 'fontweight', 'bold', 'fontsize', 16);
For more information on this function, please refer to
Make sure to replace [start_x1, start_x2, ...] and [start_y1, start_y2, ...] with the desired starting points for streamlines and specifying multiple starting points to visualize a variety of streamlines.
I hope this answers your question. Good luck!
Fareeha
on 29 Jul 2024
Edited: Walter Roberson
on 29 Jul 2024
Umar
on 29 Jul 2024
Hi @Fareeha,
After analyzing your code error, it sounds like the dimensions of the input and output matrices are incompatible. So, when you are using reshape with streamline, ensure that the dimensions match correctly, make sure that the sizes of X, Y, u, and v are compatible before reshaping. Check if the dimensions of X, Y, u, and v match the expected sizes for streamline. If needed, adjust the dimensions of the input matrices to align properly with the requirements of streamline. Hope, following these technical steps, you should be able to resolve your problem.
Umar
on 29 Jul 2024
Hi @Fareeha,
Here's how you can fix the error and update the code:
% Assuming X, Y, u, and v are defined before this point
% Check the dimensions of X and Y
sizeX = size(X);
% Reshape u and v to match the dimensions of X and Y
reshaped_u = reshape(u, sizeX);
reshaped_v = reshape(v, sizeX);
% Call streamline with the correctly reshaped arrays
streamline(X, Y, reshaped_u, reshaped_v, 0, 0);
In the corrected code snippet above, you first determine the size of X to obtain the dimensions needed for reshaping u and v. Then, reshape u and v using the dimensions of X to ensure compatibility with the streamline function call. Additionally, the error persists when using [1,1] or [1,1,1,1] as reshaping dimensions because these dimensions do not match the size of X and Y, leading to the same error message. It is crucial to reshape the arrays correctly based on the dimensions of X and Y to avoid the "number of elements must not change" error.
Fareeha
on 30 Jul 2024
Umar
on 30 Jul 2024
@Fareeha,
Could you please share your code snippet that is causing these errors. Thx
Categories
Find more on Multivariate t Distribution 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!
