Trying to fit surface into plot that is 3D

clear
close all
clc
data = load('survey.dat');
rho = data(1:9665, 1);
T = data(1:9665, 2);
p = data(1:9665, 6);
figure(1)
plot3(rho,T,p, '.', 'MarkerSize', 1, 'Color', 'r'), box 'on';
xlabel('\rho', 'FontSize', 10, 'fontweight', 'bold');
ylabel('T', 'FontSize', 10, 'fontweight', 'bold');
zlabel('p', 'FontSize', 10, 'fontweight', 'bold');
title('Equation', 'FontSize', 12);
axis([0 1.5 1 10 -1 250])
ft = fittype(strcat('rho*T + rho^2*(a01*T+a02*T^(1/2)+a03+a04/T+a05/T^2) + rho^3*(x06*T+x07+x08/T+x09/T^2)+ rho^4*(x10*T+x11+x12/T) + rho^5*(x13) + rho^6*(x14/T+x15/T^2) + rho^7*(x16/T)+ rho^8*(x17/T+x18/T^2) + rho^9*(x19/T^2)+(rho^3*(x20/T^2+x21/T^3)+rho^5*(x22/T^2+x23/T^4)+rho^7*(x24/T^2+x25/T^3)+rho^9*(x26/T^2+x27/T^4)+rho^11*(x28/T^2+x29/T^3)+rho^13*(x30/T^2+x31/T^3+x32/T^4))*exp(-3*rho^2)'),'independent',{'rho','T'},'dependent','z');
f = fit(X, Y, ft);
I am trying to understand how to fit the surface to the 3D plot i already have. I want to create a surface plot by using the custom equation from variable ft and create surface from it that will fit to the data. I can't find any help anywhere, I know I was close at some point because the surface was being drawn (which is weird because i was using only plot function and it still created 3d surface) and now I'm completely lost like a child in a fog. Any help will be greatly appreciated.

Réponses (1)

Tro JOP
Tro JOP le 7 Juil 2020
Nevermind, I actually figured it out. There is something that bugs me though.I used default plot and I got surface plot anyway... Which is odd and kinda hard to manipulate. But when I was trying to use plot3 or surf or any other type of plot then results were none.
clear
close all
clc
data = load('survey.dat');
X = data(1:9665, 1);
Y = data(1:9665, 2);
Z = data(1:9665, 6);
ft = fittype(strcat('rho*T + rho^2*(a01*T+a02*T^(1/2)+a03+a04/T+a05/T^2) + rho^3*(x06*T+x07+x08/T+x09/T^2)+ rho^4*(x10*T+x11+x12/T) + rho^5*(x13) + rho^6*(x14/T+x15/T^2) + rho^7*(x16/T)+ rho^8*(x17/T+x18/T^2) + rho^9*(x19/T^2)+(rho^3*(x20/T^2+x21/T^3)+rho^5*(x22/T^2+x23/T^4)+rho^7*(x24/T^2+x25/T^3)+rho^9*(x26/T^2+x27/T^4)+rho^11*(x28/T^2+x29/T^3)+rho^13*(x30/T^2+x31/T^3+x32/T^4))*exp(-3*rho^2)'),'independent',{'rho','T'},'dependent','z');
f = fit([X,Y],Z,ft);
view(3);
h = plot(f,[X,Y],Z); grid off, box on;
xlim([0 1.5]);
ylim([0 11]);
zlim([-1700 1100]);
xlabel('\rho', 'FontSize', 12, 'fontweight', 'bold');
ylabel('T', 'FontSize', 12, 'fontweight', 'bold');
zlabel('p', 'FontSize', 12, 'fontweight', 'bold');
title('Graph', 'FontSize', 14);
set(h,'Marker','.','MarkerEdgeColor', 'r', 'MarkerFaceColor', 'r', 'MarkerSize', 4);
colorbar;
I would be more than happy if someone was able to tell me why plot in that code worked as a surf because I have no idea. I tried to use surf initially but no matter how I used it I was getting some errors so I just went with plot because it works... No idea how but it works...

4 commentaires

John D'Errico
John D'Errico le 7 Juil 2020
We don't have your data, so there is no way to help you, or to offer advice as to if this a reasonable model.
Plot3 does not work as a call to surf, nor does plot. But plot does work using a surface fit with the curve fitting toolbox, because that toolbox overloads plot. When you plot the model created using fit, it is smart enough to know that you wanted a surface plot.
Tro JOP
Tro JOP le 7 Juil 2020
Oh I see... Sorry about no data, was kinda in the middle of it and forgot to include it. But what you wrote is very helpful for me to understand it why this works. Well I definitely learnt something I couldn't find elsewhere. Thanks for that. Luckily I was able to figure out the exercise by myself (but I was clueless to why it worked) so I'm kinda proud of that haha.
As long as it works. Some of the tools I've written overload plot. This can be useful when working with objects in MATLAB. For example,
px = [0 1 1 0 0];py = [0 0 2 1 0];
ps = polyshape(px,py);
plot(ps)
So perhaps surpringly, plot works when applied to a polyshape object in MATLAB. We could have predicted that, by taking a glance at the methods defined for a polyshape.
>> methods(ps)
Methods for class polyshape:
addboundary holes nearestvertex polybuffer rotate translate
area intersect numboundaries polyshape scale triangulation
boundary isequal numsides regions simplify turningdist
boundingbox ishole overlaps rmboundary sortboundaries union
centroid isinterior perimeter rmholes sortregions xor
convhull issimplified plot rmslivers subtract
As you can see, plot is one of them.
Just for kicks, try the same thing on a fit object, as produced by the curve fitting toolbox. You may be surprised at some of the functions that can be used.
mdl = fit(rand(10,1),rand(10,1),'poly2')
mdl =
Linear model Poly2:
mdl(x) = p1*x^2 + p2*x + p3
Coefficients (with 95% confidence bounds):
p1 = 1.213 (-3.747, 6.173)
p2 = -0.5017 (-4.58, 3.577)
p3 = 0.4511 (-0.09849, 1.001)
So some randomly garbage polynomial model now exists. What does methods tell me?
methods(mdl)
Methods for class cfit:
argnames coeffnames dependnames fitoptions integrate numcoeffs probnames type
category coeffvalues differentiate formula islinear plot probvalues
cfit confint feval indepnames numargs predint setoptions
For example:
>> differentiate(mdl,3)
ans =
6.7762
Tro JOP
Tro JOP le 7 Juil 2020
wow thank you very much, I didn't know you can list methods like that and stuff. Interesting. I just started in mathlab like few days ago and still learning plenty. Thanks for some tips and useful information, I appreciate it.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with Curve Fitting Toolbox dans Centre d'aide et File Exchange

Produits

Version

R2014b

Question posée :

le 7 Juil 2020

Commenté :

le 7 Juil 2020

Community Treasure Hunt

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

Start Hunting!

Translated by