How to make a curve line through data points on surface 3D (curve line fitting in 3D)

8 views (last 30 days)
Dear everyone,
I have a suface 3D in matlab. I want to build a curve line through data points (A, B, C, D) in this surface. I think this is the curve line fitting of surface. I try to do that but I couldn't obtain result. Please Help me for this.
The code is showed below.
Many thanks,
Best regard.
clear all;
clc;
x1 = [10,11,12,10,11,12,10,11,12,10,11,12];
x2 = [60,60,60,70,70,70,80,80,80,90,90,90];
Profit = [133.355, 151.273, 143.703, 201.625, 218.227, 209.948, 219.271, 246.907, 233.791, 215.890, 246.132, 228.042];
xv = 10:0.1:12;
yv =60:1:90;
[X,Y] = ndgrid(xv, yv);
Z = griddata(x1, x2, Profit, X, Y, 'cubic');
figure
surf(X,Y,Z)
grid on
hold on
A=scatter3(x1(8),x2(8),Profit(8),'filled','MarkerFaceColor', 'y')
B=scatter3(x1(2),x2(2),Profit(2),'filled','MarkerFaceColor', 'y')
C=scatter3(x1(5),x2(5),Profit(5),'filled','MarkerFaceColor', 'y')
D=scatter3(x1(11),x2(11),Profit(11),'filled','MarkerFaceColor', 'y')
xlabel('Скорость, м/с'), ylabel('Грузоподъемность, т'), zlabel('Profit, 10^4*$')
view(-120,47)
  2 Comments
Ameer Hamza
Ameer Hamza on 8 May 2020
Can you show an example of your expected output? How do you want to draw the curved line?
Dam Tung
Dam Tung on 9 May 2020
Hi, Ameer Hamza, I want to get result as in the picture. Please help me with my codes. Many thanks

Sign in to comment.

Accepted Answer

darova
darova on 9 May 2020
Edited: darova on 9 May 2020
Try this solution
x0 = x1([8 2 5 11]);
y0 = x2([8 2 5 11]);
yy = linspace(min(y0),max(y0),20);
xx = interp1(y0,x0,yy);
zz = interp2(X',Y',Z',xx,yy);
line(xx,yy,zz,'linew',2)
result

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 8 May 2020
Hi,
Since you are plotting 3D plot of data, probably you're looking for a surface fit model. Then it is easy to use: cftool and select X, Y, Z and obtain the fit. Here is a sample code - a cubic fit generated by the cftool (Curve Fitting) toolbox.
function [fitresult, gof] = createFit(X, Y, Z)
%CREATEFIT(X,Y,Z)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input : X
% Y Input : Y
% Z Output: Z
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 08-May-2020 16:05:32
%% Fit: 'untitled fit 1'.
[xData, yData, zData] = prepareSurfaceData( X, Y, Z );
% Set up fittype and options.
ft = 'cubicinterp';
% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft, 'Normalize', 'on' );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, [xData, yData], zData );
legend( h, 'untitled fit 1', 'Z vs. X, Y', 'Location', 'NorthEast' );
% Label axes
xlabel X
ylabel Y
zlabel Z
grid on
Good luck
  1 Comment
Dam Tung
Dam Tung on 9 May 2020
Dear mr. Sulaymon Eshkabilov
Thank you for your answer. Can you hel me with my code, please? I want to get result as in the below picture:
Best regard.

Sign in to comment.

Categories

Find more on Get Started with Curve Fitting Toolbox 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!