plotting Graph in 3D help

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

Umar
Umar on 4 Jul 2024
Edited: Walter Roberson on 29 Jul 2024
Hi Fareeha,
You asked, i want to plot this graph in 3D, how can I?
In order to do that, you can use plot3 function to achieve your goal. For more information on this function, please refer to
Let me know if this answers your question.

12 Comments

Fareeha
Fareeha on 4 Jul 2024
Edited: Fareeha on 4 Jul 2024
thank you @Umar for your response but i dont understand how to use plot3 command for this particular code. As i have values for x and y but for z what values are to be used?
Hi Fareeha,
To modify the code for 3D plotting using plot3, you would need to have additional data representing a third dimension. For instance, if you have a third variable z that corresponds to the velocity values, you can modify the plotting section as follows.
% Assuming z represents the third dimension data
z = sin(x); % Example third dimension data
figure(2)
plot3(x, y(2, :), z, 'linewidth', 1.5);
xlabel('\eta', 'fontweight', 'bold', 'fontsize', 16)
ylabel('f^{\prime}(\eta)', 'fontweight', 'bold', 'fontsize', 16)
zlabel('z-axis Label', 'fontweight', 'bold', 'fontsize', 16)
In this modified code snippet, plot3 is used to create a 3D plot with x, y(2, :), and z representing the three dimensions. You can adjust the z values based on your specific data.Remember, when transitioning from 2D to 3D plotting, ensure that your data is appropriately structured to represent the third dimension accurately for meaningful visualization using plot3.
Fareeha
Fareeha on 4 Jul 2024
Edited: Fareeha on 4 Jul 2024
@Umar what if i don;t know what is z, then? can you help me understanding with an example using same code?
@Umar does contour command work for this?
Hi Fareeha,
To answer your question about z in your example code, it will be set to zeros to plot the data in 3D space.
% Plotting the velocity in 3D
figure(1)
% Plotting in 3D
plot3(x, y(2, :), zeros(size(x)), 'linewidth', 1.5);
Unrecognized function or variable 'x'.
hold on
xlabel('\eta', 'fontweight', 'bold', 'fontsize', 16)
ylabel('f^{\prime}(\eta)', 'fontweight', 'bold', 'fontsize', 16)
zlabel('Z-axis Label') % Adding Z-axis label
Also, in the provided function basic, which involves solving a boundary value problem using bvp4c, the primary focus seems to be on plotting the velocity profile rather than generating contour plots.
Here is a full version of your code by graphing plot in 3D.
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 the velocity in 3D
figure(1)
% Plotting in 3D
plot3(x, y(2, :), zeros(size(x)), 'linewidth', 1.5);
hold on
xlabel('\eta', 'fontweight', 'bold', 'fontsize', 16)
ylabel('f^{\prime}(\eta)', 'fontweight', 'bold', 'fontsize', 16)
zlabel('Z-axis Label') % Adding Z-axis label
% 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
In the updated code snippet, the plot3 function is used to create a 3D plot of the velocity by enhanceing visualization by adding a third dimension to the plot. In this case, the velocity data is plotted in 3D space, providing a more comprehensive view of the results. Additionally, a Z-axis label is added to the plot for clarity. The rest of the code remains the same, defining parameters, handling boundary conditions, and solving the system of first-order ODEs as before. Your bc2D function still calculates the residual of the boundary conditions, while the bvp2D function defines the system of ODEs to be solved using the bvp4c solver.
Hope this will help resolve your problem.
Thankyou @Umar I also want to generate contour plot. how can i adjust these values to generate contour?
@Umar i also want to use a loop for generating graph for different values of parameters. like i want to variate K from 1 to 3 for fixed values of n and lam, can you help me doing this?
Hi Fareeha,
To modify the existing code for generating a contour plot, we need to adjust the plotting section and include the necessary commands to create a contour plot. Below is the updated code with modifications for generating a contour plot:
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 the velocity in 3D
figure(1)
% Plotting in 3D
plot3(x, y(2, :), zeros(size(x)), 'linewidth', 1.5); hold on
xlabel('\eta', 'fontweight', 'bold', 'fontsize', 16)
ylabel('f^{\prime}(\eta)', 'fontweight', 'bold', 'fontsize', 16)
zlabel('Z-axis Label') % Adding Z-axis label
% Generating Contour Plot
figure(2)
contourf(x, y(2, :), 'LineWidth', 1.5); % Creating a filled contour plot
colorbar; % Adding a color bar for reference
xlabel('\eta', 'fontweight', 'bold', 'fontsize', 16)
ylabel('f^{\prime}(\eta)', 'fontweight', 'bold', 'fontsize', 16)
title('Contour Plot') % Adding a title to the plot
% 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
In this updated code, a new figure (Figure 2) is created to display the contour plot using the contourf function. The contourf function generates a filled contour plot based on the values of x and y(2, :). Additionally, a color bar is added to provide a reference for the contour levels.
By incorporating these modifications, the code will now produce a contour plot alongside the existing 3D plot, offering a visual representation of the data in a different format.
You also asked, i also want to use a loop for generating graph for different values of parameters. like i want to variate K from 1 to 3 for fixed values of n and lam, can you help me doing this?
You can incorporate the following code into your project, where loop iterates over the values of K from 1 to 3. For each iteration, the boundary value problem is solved using bvp4c with the corresponding value of K. The resulting solution is then plotted in 3D.
for K = 1:3
sol = bvp4c(@(t, y) bvp2D(t, y, K, n, lam), @bc2D, sol1);
x = sol.x;
y = sol.y;
% Plotting in 3D
plot3(x, y(2, :), zeros(size(x)), 'linewidth', 1.5);
end
Fareeha
Fareeha on 6 Jul 2024
Edited: Fareeha on 6 Jul 2024
@Umar thank you, I executed this code and found this error:
Error using contourf (line 57)
Z must be at least a 2x2 matrix.
Error in basic(line 10)
contourf(x, y(2, :), 'LineWidth', 1.5);

Hi Fareeha,

To fix the error, ensure that the input matrix Z passed to the contourf function is at least a 2x2 matrix. Here's an example to demonstrate how to fix the error:

% Create sample data

x = 1:5; y = 1:5; [X, Y] = meshgrid(x, y);

% Example data, replace with your own data

Z = peaks(5);

% Plot the contour

contourf(X, Y, Z, 'LineWidth', 1.5);

Please see attached plot.

In this example, X and Y are 2D matrices created using meshgrid, and Z is a sample 5x5 matrix. By ensuring that Z is a valid 2D matrix, the error should be resolved when calling contourf.

Let me know if you need further assistance or help.

Fareeha
Fareeha on 7 Jul 2024
Edited: Fareeha on 7 Jul 2024
@Umar thank you, i have just used z=peak(); in my code and able to generate contour.. I am attaching figure for reference.. can you tell how to check whether i am getting right figure?
can i use Z=peak(); in my code?

Sign in to comment.

More Answers (1)

Umar
Umar on 7 Jul 2024
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

@Umar now i need to draw streamlines for the same code. can you tell me how to code streamlines using the earlier provided code? i appreciate your support in this regard.
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!
Thank you @Umar, i got this error
Error using reshape
To RESHAPE the number of elements must not change.
Error in contour_plot_Magnetic_C (line 23)
streamline(X, Y, reshape(u, size(X)), reshape(v, size(X)), 0,0);
i have tried these as well but got same error
streamline(X, Y, reshape(u, size(X)), reshape(v, size(X)), [1,1], [1,1]);
streamline(X, Y, reshape(u, size(X)), reshape(v, size(X)), [1,1,1,1], [1,1,1,1]);
Can you suggest a solution. thannk you
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.
Fareeha
Fareeha on 29 Jul 2024
Edited: Fareeha on 29 Jul 2024
@Umar how to match the sizes?
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.
@Umar i tried it and now i got this error:
Error using stream2 (line 46)
U,V must all be size 2x2 or greater.
Error in streamline (line 62)
verts = stream2(x,y,u,v,sx,sy,options);
Error in contour_plot_Magnetic_C (line 34)
streamline(X, Y, reshaped_u, reshaped_v, 0, 0);
i am using streamline function neither verts nor stream2.. then why i am getting this errror?
@Fareeha,
Could you please share your code snippet that is causing these errors. Thx

Sign in to comment.

Products

Release

R2018b

Asked:

on 4 Jul 2024

Commented:

on 30 Jul 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!