How I can plot a funtion of three variables r, z and t. What is the algorithm to plot such type of difficult functions.
15 次查看(过去 30 天)
显示 更早的评论
How I can plot a funtion of three variables r, z and t. What is the algorithm to plot such type of difficult functions.
2 个评论
Sam Chak
2023-12-23
It depends on what you really want to plot. In this example, the volume of a cuboid is a function of three parameters, which is the product of its dimensions: length (x), width (y), and height (z): .
Can you describe how exactly you would like to visualize the volume of a cuboid in graphical representation? This will provide insights into determining which plotting functions in MATLAB should be used.
回答(1 个)
TED MOSBY
2024-9-1
Hi,
To plot a data having 3 variables which I assume you mean 3 inputs and 1 output which is a 4D data, it is difficult to visualise it but I found some functions in MATLAB with which you can visualise it:
1. Slice Plots
You can visualize the function by fixing one of the variables and plotting slices of the function in the remaining two-variable space. For example, you can fix ( t ) and plot ( f(r, z, t) ) as a 2D surface plot for different values of ( t ).
% Define your function
f = @(r, z, t) r.^2 + z.^2 + t.^2; % Example function
% Define ranges for each variable
r = linspace(-10, 10, 100);
z = linspace(-10, 10, 100);
t_values = [-5, 0, 5]; % Different t values to slice
[R, Z] = meshgrid(r, z);
figure;
for i = 1:length(t_values)
t = t_values(i);
F = f(R, Z, t);
subplot(1, length(t_values), i);
surf(R, Z, F);
title(['t = ', num2str(t)]);
xlabel('r');
ylabel('z');
zlabel('f(r, z, t)');
shading interp;
end
2. Isosurfaces
If you want to visualize the function as a 3D volume, you can use isosurfaces. This method shows surfaces where the function has a constant value.
% Define the function as an anonymous function
f = @(r, z, t) r.^2 + z.^2 + t.^2; % Example function
% Define a grid
r = linspace(-10, 10, 50);
z = linspace(-10, 10, 50);
t = linspace(-10, 10, 50);
[R, Z, T] = meshgrid(r, z, t);
% Compute the function values on the grid
F = f(R, Z, T);
% Plot isosurfaces
figure;
isosurface(R, Z, T, F, 50); % 50 is the isovalue
xlabel('r');
ylabel('z');
zlabel('t');
title('Isosurface of f(r, z, t)');
axis equal;
3. Animated Plots
You can create an animation by varying one of the variables over time, effectively creating a dynamic 3D plot.
% Define the function as an anonymous function
f = @(r, z, t) r.^2 + z.^2 + t.^2; % Example function
% Define the range for r and z
r = linspace(-10, 10, 100);
z = linspace(-10, 10, 100);
% Create a 2D grid for r and z
[R, Z] = meshgrid(r, z);
% Define the range for t
t_values = linspace(-10, 10, 50);
% Create a figure for the animation
figure;
% Loop through each t value to create the animation
for t = t_values
% Evaluate the function at the current value of t
F = f(R, Z, t);
% Plot the surface
surf(R, Z, F);
xlabel('r');
ylabel('z');
zlabel('f(r, z, t)');
title(['t = ', num2str(t)]);
shading interp; % Optional: make the surface look smoother
axis([-10 10 -10 10 0 400]); % Adjust axis limits as needed
grid on;
% Pause to control the speed of the animation
pause(0.1);
end
4. Contour Slices
Another approach is to use contour slices, which can give you a sense of the function's behavior in different planes.
% Define the function as an anonymous function
f = @(r, z, t) r.^2 + z.^2 + t.^2; % Example function
% Define a grid
r = linspace(-10, 10, 50);
z = linspace(-10, 10, 50);
t = linspace(-10, 10, 50);
[R, Z, T] = meshgrid(r, z, t);
% Compute the function values on the grid
F = f(R, Z, T);
% Plot contour slices
figure;
slice(R, Z, T, F, [], [], t_values); % Slices at specified t values
xlabel('r');
ylabel('z');
zlabel('t');
title('Contour Slices of f(r, z, t)');
colorbar;
Try out these methods, hope this helps!
To know more about "meshgrid" refer the following documentation:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!