Find gradient ascent for 3D surface
7 views (last 30 days)
Show older comments
- I have a 3D surface (x,y,z) plotted using the following code (excel sheet attached):
clear; clc; clearvars;
% Read table:
T = readtable('full-nov26th.csv');
rows = (T.(3)== 3);
T = T(rows, :);
% Define Variables
x = T.(1);
y = T.(2);
z = T.(4);
xi = linspace(min(x),max(x), 200);
yi = linspace(min(y),max(y), 200);
[X,Y] = meshgrid(xi,yi);
Z = griddata(x,y,z,X,Y);
surf(X,Y,Z);
- The generated surface looks as follows:
- I am trying to plot a line that incrementally goes from the starting point (0,0,14.19) to the highest point on the surface.
- I have seen other answers on the forums dealing with analytical functions. However, in here, I only have the surface generated from points. Hence, how could I be able to do it from these points?
0 Comments
Accepted Answer
Star Strider
on 28 Nov 2021
This code finds the maximum of the surface and then uses griddedInterpolant with the calculated ‘xv’ and ‘yv’ vectors to draw the interpolated line, following the ‘Z’ contours.
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/815814/full-nov26th.csv', 'VariableNamingRule','preserve')
rows = (T.(3)== 3);
T = T(rows, :);
% Define Variables
x = T.(1);
y = T.(2);
z = T.(4);
xi = linspace(min(x),max(x), 200);
yi = linspace(min(y),max(y), 200);
[X,Y] = ndgrid(xi,yi);
Z = griddata(x,y,z,X,Y);
zmix = find(Z == max(Z(:))) % There Is More Than One Maximum
Xc = X(zmix);
Yc = Y(zmix);
Zc = Z(zmix);
Xcm = mean(Xc);
Ycm = mean(Yc);
Zcm = mean(Zc);
xv = linspace(0, Xcm, 100);
yv = linspace(0, Ycm, 100);
Zi = griddedInterpolant(X,Y,Z);
zv = Zi(xv,yv);
figure
surf(X,Y,Z, 'EdgeColor','none', 'FaceAlpha',0.5);
hold on
stem3(Xcm,Ycm,Zcm,'-^r', 'filled')
plot3(xv, yv, zv, '-m', 'LineWidth',2)
hold off
colormap(turbo)
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Original: Complete Surface & Requested Line')
figure
surf(X,Y,Z, 'EdgeColor','none', 'FaceAlpha',0.5);
hold on
stem3(Xcm,Ycm,Zcm,'-^r', 'filled')
plot3(xv, yv, zv, '-m', 'LineWidth',2)
hold off
colormap(turbo)
xlabel('X')
ylabel('Y')
zlabel('Z')
xlim([0 2*Xcm])
ylim([0 2*Ycm])
zlim([10 20])
view(290,30)
title('Detail: Zoomed Surface Section & Requested Line')
Choose different lime colours & colormap options to get the desired result.
.
2 Comments
More Answers (1)
Alan Weiss
on 28 Nov 2021
You might be interested in this example: Pattern Search Climbs Mount Washington
Alan Weiss
MATLAB mathematical toolbox documentation
0 Comments
See Also
Categories
Find more on Surface and Mesh Plots 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!