How I can plot a funtion of three variables r, z and t. What is the algorithm to plot such type of difficult functions.

조회 수: 5 (최근 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
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
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:

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by