Find gradient ascent for 3D surface

6 visualizzazioni (ultimi 30 giorni)
HAMID
HAMID il 28 Nov 2021
Commentato: Star Strider il 29 Nov 2021
  • 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?

Risposta accettata

Star Strider
Star Strider il 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')
T = 161599×6 table
in:xrot in:yrot in:n out:PE out:ARM out:r _______ _______ ____ ______ _______ _____ 0 0 3 14.19 0 0.63 0.01 0 3 14.19 0.33 0.63 0.02 0 3 14.19 0.67 0.63 0.03 0 3 14.2 1 0.63 0.04 0 3 14.21 1.33 0.63 0.05 0 3 14.22 1.67 0.63 0.06 0 3 14.24 2 0.63 0.07 0 3 14.25 2.33 0.63 0.08 0 3 14.27 2.67 0.63 0.09 0 3 14.29 3 0.63 0.1 0 3 14.32 3.34 0.63 0.11 0 3 14.35 3.65 0.63 0.12 0 3 14.38 3.95 0.63 0.13 0 3 14.41 4.21 0.63 0.14 0 3 14.44 4.46 0.63 0.15 0 3 14.48 4.68 0.63
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
zmix = 4×1
17202 17402 18005 18204
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 Commenti
HAMID
HAMID il 29 Nov 2021
Thank you so much. This is extremely helpful.
Star Strider
Star Strider il 29 Nov 2021
As always, my pleasure!
.

Accedi per commentare.

Più risposte (1)

Alan Weiss
Alan Weiss il 28 Nov 2021
You might be interested in this example: Pattern Search Climbs Mount Washington
Alan Weiss
MATLAB mathematical toolbox documentation

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by