Someone that can help me with 3D graph?

51 views (last 30 days)
Pablo
Pablo on 10 Nov 2024 at 22:23
Commented: Umar on 12 Nov 2024 at 2:31
Hi, I'm Pablo, I'm starting with this fantastic program and now I wanted to make some graphs of surface .... I want to represent the values of a virus (z), depending on the temperature (x) and pH (y), I have tried different interpolations... the one that works best for me is v4, but the z values represented were higher than the limits of my vector, when I have marked the limits that I want to represent there is an abrupt cut in the surface, when I have solved it, and I add the z points in the surface, I see that the z data do not correspond to the temperature (x) and pH (y) that I had in my data set...I understand that it is because of small errors in the interpolation.... could someone help me to make the surface graph correctly? I attach the data in the form of column vectors. Thanks in advance
x=[ 13.6 11.3 9.7 9.3 10.6 9.8 25.6 28.2 31.4 28.2 28.6 29.2 17.9 16.7 17.1 22 19.3 17.1 7.4 7.6 7.3 9.7 9.6 15.6 14.6 15.8 15 13.4 ];
y=[ 6.77 7.2 6.83 6.69 6.76 6.67 7.87 7.87 7.84 7.98 8.33 7.78 8.03 7.99 8.02 8 8.03 8 6.68 6.24 5.84 7.26 6.5 7.97 8.24 8.03 7.9 7.97 ];
z=[ 2.31 1.71 3.52 2.45 2.6 1.89 4.59 3.87 3.47 2.85 2.17 3.47 6.44 5.86 3.91 5.71 3 3.57 3.1 3.01 3.29 3.8 2.94 5.72 4.26 4.05 5.24 6.55 ];

Accepted Answer

Umar
Umar on 11 Nov 2024 at 1:43
Edited: Umar on 11 Nov 2024 at 1:46

Hi @Pablo,

To achieve the desired visualization and reviewing the documentation provided at the link below, please see the code along with a detailed explanation of each section.

https://www.mathworks.com/help/matlab/ref/surfc.html

% Define the data
x = [13.6, 11.3, 9.7, 9.3, 10.6, 9.8, 25.6, 28.2, 31.4, 28.2, 28.6,     29.2, 17.9,   16.7, 17.1, 22, 19.3, 17.1, 7.4, 7.6, 7.3, 9.7, 9.6,     15.6, 14.6, 15.8, 15,   13.4];
y = [6.77, 7.2, 6.83, 6.69, 6.76, 6.67, 7.87, 7.87, 7.84, 7.98,     8.33, 7.78,   8.03, 7.99, 8.02, 8, 8.03, 8, 6.68, 6.24, 5.84, 7.26,     6.5, 7.97, 8.24, 8.03,   7.9, 7.97];
z = [2.31, 1.71, 3.52, 2.45, 2.6, 1.89, 4.59, 3.87, 3.47, 2.85,     2.17, 3.47,   6.44, 5.86, 3.91, 5.71, 3, 3.57, 3.1, 3.01, 3.29, 3.8,     2.94, 5.72, 4.26, 4.05,   5.24, 6.55];
% Create a grid for interpolation
[xq, yq] = meshgrid(linspace(min(x), max(x), 100), linspace(min(y),      max(y), 100));
zq = griddata(x, y, z, xq, yq, 'v4');
% Create the surface plot
figure;
surfc(xq, yq, zq);
xlabel('Temperature (°C)');
ylabel('pH');
zlabel('Log Concentration of Virus (g/L)');
title('3D Surface Plot of Virus Concentration');
colorbar; % Add a color bar
% Adjust surface properties
shading interp; % Interpolates colors across the surface
alpha(0.7); % Set transparency
% Hold on to add the original data points
hold on;
% Plot the original data points as blue dots
plot3(x, y, z, 'bo', 'MarkerSize', 8, 'MarkerFaceColor', 'b');
% Release the hold
hold off;
% Set view angle for better visualization
view(45, 30); % Adjust the view angle for better perspective

Please see attached.

In the above provide code snippet, the original data for temperature (x), pH (y), and log concentration of viruses (z) remains unchanged. This data serves as the foundation for our surface plot. The meshgrid function generates a grid of points over which we will interpolate the z values. The linspace function is used to create evenly spaced values between the minimum and maximum of x and y. The griddata function is employed to interpolate the z values over the grid defined by xq and yq. The method v4 is used for smooth interpolation.The surfc function creates a 3D surface plot. The axes are labeled appropriately, and a color bar is added to indicate the scale of the log concentration. The shading interp command smooths the color transitions across the surface, while alpha(0.7) sets the transparency of the surface, allowing for better visibility of the underlying data points. The hold on command allows us to overlay additional plots on the existing figure. The plot3 function is used to plot the original data points as blue dots (bo), with specified marker size and face color. Finally, the view function is called to set the angle of the 3D plot, enhancing the visual perspective of the data.

If you have any further questions, please let me know.

  2 Comments
Pablo
Pablo on 11 Nov 2024 at 19:41
Hi Umar, thanks for your answer, the first tests I did led me to the same result as you, but, I have to say that your modification in the viewing angle was very successful, the graphic looks very good! Thank you! But I still have the following question: why do I still see a peak on the surface, which would correspond to a value of z=8 approximately, when my z vector does not exceed 6 Log? It is due to the error introduced when interpolating with the function v4, isn't it? If we look, there is also a part of the surface where at T(ºC)=30 and pH=6 (approximately), both the contour and the surface show z values between 7 and 8. How can this situation be explained? Thank you very much for your help Umar.
Umar
Umar on 12 Nov 2024 at 2:31

Hi @Pablo,

After going through your comments, please see my response below.

Understanding Interpolation with griddata

The griddata function in MATLAB is designed to perform interpolation on scattered data points. It utilizes various methods (such as linear, nearest neighbor, natural neighbor, cubic, and v4) to estimate values at query points based on the known values at sample points. Each method has its characteristics:

Linear and Cubic: These methods create surfaces that can be more sensitive to the arrangement of input data points.

V4 (Biharmonic spline): This method is particularly notable for producing smoother surfaces but may introduce artifacts or overshoots in regions where data density is low.

Convex Hull: Interpolation with griddata is constrained by the convex hull of the sample data. If query points fall outside this region or are poorly represented by nearby sample points, it can lead to extrapolated values or peaks that don't align with the input constraints.

Data Density and Distribution: Sparse or unevenly distributed sample points can lead to misleading interpolated values. If there are few points surrounding certain areas (like your z-values), the algorithm may generate peaks as it tries to smoothly connect these sparse points.

Explanation of Observed Peak at z ≈ 8

The peak you observe at approximately z = 8 could be attributed to several factors:

Interpolation Error: If your data does not adequately cover all regions of interest (especially around the temperatures and pH values you mentioned), the interpolation algorithm may produce exaggerated values as it attempts to create a smooth transition between known data points.

Smoothing Effects: The "v4" method might be overly smoothing your surface, causing it to generate peaks that are not present in the actual dataset. This is particularly true if there are abrupt changes in your data's behavior that are not captured well by surrounding sample points.

Boundary Effects: If the input values for temperature (T) and pH are near the edges of your sampled range, boundary effects could cause unexpected peaks in interpolation results.

My recommendations are listed below.

Check Sample Distribution: Ensure that your sample data covers the entire range of interest uniformly. Consider increasing sample density in regions where you observe anomalies.

Experiment with Different Methods: Try using different interpolation methods available within `griddata`, such as linear or cubic, and compare results to see if they yield more reasonable outputs without unexpected peaks.

Use Visualization Tools: Utilize contour plots or mesh visualizations to better understand how your interpolated surface behaves across different ranges and identify areas where peaks occur relative to your sample data distribution.

Normalize Data: If scaling issues exist between x, y, and z values, consider normalizing your data before performing interpolation to minimize distortion effects.

Investigate Local Behavior: Analyze local neighborhoods around critical query points (like those corresponding to T = 30°C and pH = 6) to understand how nearby samples influence interpolated results.

In nutshell, while interpolation can provide valuable insights from scattered data, it is essential to recognize its limitations and potential for introducing artifacts. Carefully managing sample distribution and choosing appropriate methods will help mitigate issues like unexpected peaks in interpolated surfaces. Further exploration into local variations and thorough testing with various interpolation techniques will enhance your understanding and improve outcomes in future analyses.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 10 Nov 2024 at 23:17
It is not a nice surface.
x=[ 13.6 11.3 9.7 9.3 10.6 9.8 25.6 28.2 31.4 28.2 28.6 29.2 17.9 16.7 17.1 22 19.3 17.1 7.4 7.6 7.3 9.7 9.6 15.6 14.6 15.8 15 13.4 ];
y=[ 6.77 7.2 6.83 6.69 6.76 6.67 7.87 7.87 7.84 7.98 8.33 7.78 8.03 7.99 8.02 8 8.03 8 6.68 6.24 5.84 7.26 6.5 7.97 8.24 8.03 7.9 7.97 ];
z=[ 2.31 1.71 3.52 2.45 2.6 1.89 4.59 3.87 3.47 2.85 2.17 3.47 6.44 5.86 3.91 5.71 3 3.57 3.1 3.01 3.29 3.8 2.94 5.72 4.26 4.05 5.24 6.55 ];
[xmin, xmax] = bounds(x);
[ymin, ymax] = bounds(y);
X = linspace(xmin, xmax);
Y = linspace(ymin, ymax);
F = scatteredInterpolant(x(:), y(:), z(:));
[XG, YG] = ndgrid(X,Y);
ZG = F(XG, YG);
surf(XG, YG, ZG)
hold on
plot3(x, y, z, 'r*');
hold off

Tags

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!