Re: Surface fitting error while using Fit
- To: mathgroup at smc.vnet.net
- Subject: [mg96569] Re: Surface fitting error while using Fit
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Mon, 16 Feb 2009 07:00:38 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <gn8jb8$7oa$1@smc.vnet.net>
In article <gn8jb8$7oa$1 at smc.vnet.net>, Srikanth <skt at xdtech.com>
wrote:
> I am using mathematica to fit data to a surface. My data is in a
> matrix form:
> Data1={{1.1, 7, 8.765}, {1.1, 7.5, 10.6481},.....,{1.9, 10.5,
> 26.1048}}
>
> I want to get a form z=f(x,y) that fits this data, with x and y being
> my first and second element of each vector. I expect the solution to
> be a linear combination of x, y, xy and some constant term.
>
> So I tried
> Fit[Data1, {1, x, y, xy}, {x, y}]
>
> and I get an error message:
> Fit::fitm: Unable to solve for the fit parameters; the design matrix
> is nonrectangular, non-numerical, or could not be inverted.
>
> I'm not sure what it meant by non-numerical (for example Data1[[1,1]]
> gives 1.1 as expected). The data is rectangular and well defined (by
> which I mean I never have cases like {x, , z} with y missing or
> anything absurd like that). It cannot be inverted of course, but that
> didn't seem to be a problem from the example 9 given here:
> http://reference.wolfram.com/mathematica/tutorial/CurveFitting.html
>
> Any suggestions on what I need to do?
It is very likely that some of the matrix entries are represented as
*String* rather than as numbers, which means that, by visual inspection,
you see them as numbers --- since, by default, Mathematica does not
display the double-quote characters that surround a string --- but they
are represented internally as string of characters, indeed.
Among many other ways offered by Mathematica, you can check whether your
matrix contains only numeric expressions with
MatrixQ[data1, NumericQ]
and you can convert strings into numbers with
data1 /. s_String :> ToExpression[s]
For instance,
In[1]:= data1 = {{1.1, 7, 8.765}, {1.1, "7.5", 10.6481}, {1.9, 10.5,
26.1048}}
Dimensions[data1]
MatrixQ[data1, NumericQ]
Fit[data1, {1, x, y, x*y}, {x, y}]
data1 = data1 /. s_String :> ToExpression[s]
MatrixQ[data1, NumericQ]
Fit[data1, {1, x, y, x*y}, {x, y}]
Out[1]= {{1.1, 7, 8.765}, {1.1, "7.5", 10.6481}, {1.9, 10.5,
26.1048}}
Out[2]= {3, 3}
Out[3]= False
During evaluation of In[1]:= Fit::fitm: Unable to solve for the fit \
parameters; the design matrix is nonrectangular, non-numerical, or \
could not be inverted. >>
Out[4]= Fit[{{1.1, 7, 8.765}, {1.1, "7.5", 10.6481}, {1.9, 10.5,
26.1048}}, {1, x, y, x y}, {x, y}]
Out[5]= {{1.1, 7, 8.765}, {1.1, 7.5, 10.6481}, {1.9, 10.5, 26.1048}}
Out[6]= True
Out[7]= -10.6512 - 6.31562 x + 2.56005 y + 1.0965 x y
Regards,
--Jean-Marc