# Curve fitting f(x,y) result

19 views (last 30 days)
ZazOufUmI on 19 Apr 2012
Commented: Walter Roberson on 28 Feb 2019
Hi,
I used Curve fitting tool to get the equation of the regression line of my area. So I have some X and Y which give some Z.
The curve fitting tool provide me a linear model like this :
Linear model Poly54:
f(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p30*x^3 + p21*x^2*y+ p12*x*y^2 + p03*y^3 + p40*x^4 + p31*x^3*y + p22*x^2*y^2+ p13*x*y^3 + p04*y^4 + p50*x^5 + p41*x^4*y + p32*x^3*y^2+ p23*x^2*y^3 + p14*x*y^4
But when I try to obtain a Z value from this equation it's not good at all. For example with X = 60 and Y = 60 , Z = 300 but with the formula I have a result like xxxE+14
Actually, I'm looking for an equation which can provide me a Z value whatever X and Y such as f(x,y) = z The area looks like this : http://www.hostingpics.net/viewer.php?id=329914example.jpg
Thank you if you can help

Richard Willey on 20 Apr 2012
I'm attaching code that shows a couple different ways to solve your problem.
I prefer the second option. The R^2 is slightly better and the model is simplier. (Note that option 2 as coded uses Statistics Toolbox rather than Curve Fitting Toolbox)
%%Input some data
x = [0 10 20 30 35 40 45 50 55 60];
y = [60 120 180 240 300];
z = [299.8 299.1 296.9 293.4 291.4 289.9 288.5 283.3 276.4 272.1;
299.8 299.1 296.9 293.4 291.4 289.9 288.5 283.3 276.4 272.1;
299.8 299.1 296.9 293.4 291.4 289.9 288.5 283.3 276.4 272.1;
299.8 299.1 296.9 293.4 291.4 289.9 288.5 283.3 276.4 272.1;
299.8 299.1 296.9 293.4 291.4 289.9 288.5 283.3 276.4 272.1];
% Observe that the data is constant in one dimension.
% The variable Y has no predictive power
% This means we can treat this as a curve fitting problem rather
% than a surface fitting problem
x = x';
z = mean(z, 1)';
% Visualize our data
scatter(x,z)
%%Generate a fit
% Option one
[foo, GoF] = fit(x,z, 'poly2')
hold on
plot(foo)
%%Option two: Fit a piecewise linear model
% Looking at the data, it appears as if we might generate a better fit by
% assuming a piecewise linear model with a "kink" at the 7th data point
% Start by centering the data such that the 7th data point is equal to zero
New_x = x - 45;
% Create a dummy variable that flags whether we are before or after the 7th
% data point
dummy = New_x > 0
% Create an interaction term
interaction = New_x .* dummy
% Generate our regression model
X = [ones(size(x)), New_x, interaction];
b = regress(z, X)
plot(x, X*b)
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 28 Feb 2019
You should give the command
format long g
and then you should display b again.

ZazOufUmI on 20 Apr 2012
Should I use fittype for my problem ?

Richard Willey on 20 Apr 2012
Hi there
The "Poly54" fit type is specifying a 5th order polynomial in one direction and a 4th order polynomial in the other.
From the looks of things, you have roughly 40 data points. Your model has 20 terms (many of which are high order). I suspect that you're overfitting like crazy, which means that your model can perform very poorly if used to generate a prediction for out of sample data.
Based on the chart that you provided, you can probably cobble together a simple custom equation with a linear model in one direction and a cubic in the other. Something like
Y = b0 + b1*X1 + b2*X2^3

ZazOufUmI on 20 Apr 2012
I tried with a Poly31 and here is the result : http://www.hostingpics.net/viewer.php?id=453193result.jpg
For example with a X = 60 and Y = 60 I have a Z = 272 but with this formula I obtain Z = -2.54517e+05
I don't know what is wrong.

Richard Willey on 20 Apr 2012
Hi ZazOufUMl
A "Poly31" model is different than the one that I suggested. (Poly31 will contain a number of cross terms).
Any chance that you can post your raw X, Y, and Z data?
Walter Roberson on 20 Apr 2012

ZazOufUmI on 20 Apr 2012
I'm sorry if I didn't understand your answer. Here is my data :
%%%%%%%%%%%%%%%Data%%%%%%%%%%%%%%%
x = [0 10 20 30 35 40 45 50 55 60];
y = [60 120 180 240 300];
z = [299.8 299.1 296.9 293.4 291.4 289.9 288.5 283.3 276.4 272.1;
299.8 299.1 296.9 293.4 291.4 289.9 288.5 283.3 276.4 272.1;
299.8 299.1 296.9 293.4 291.4 289.9 288.5 283.3 276.4 272.1;
299.8 299.1 296.9 293.4 291.4 289.9 288.5 283.3 276.4 272.1;
299.8 299.1 296.9 293.4 291.4 289.9 288.5 283.3 276.4 272.1];
Thank you

ZazOufUmI on 23 Apr 2012
Hi Richard,
Thank you, your solution works well but I have a last question. Unfortunately as you said, we lost the Y parameter, but how can I do to consider it ? Because my raw data looks like this : http://www.hostingpics.net/viewer.php?id=629034table.jpg
As you can see my Y data are differents depending X and Z. Maybe I grabbed my data in a wrong way ? Do you think it's possible to consider this parameter ?
Thank you

### Categories

Find more on Linear and Nonlinear Regression 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!