Find gradient ascent for 3D surface
조회 수: 6 (최근 30일)
이전 댓글 표시
- 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
채택된 답변
Star Strider
2021년 11월 28일
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.
.
추가 답변 (1개)
Alan Weiss
2021년 11월 28일
You might be interested in this example: Pattern Search Climbs Mount Washington
Alan Weiss
MATLAB mathematical toolbox documentation
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!