surface fitting

5 views (last 30 days)
smp
smp on 29 Nov 2011
I am using MATLAB 2007. I want to do surface fitting. Any help will be appreciated.
Thanks.

Accepted Answer

Grzegorz Knor
Grzegorz Knor on 29 Nov 2011
See:
You can also write your own function e.g. by using the Optimization Toolbox.

More Answers (7)

smp
smp on 29 Nov 2011
Thanks.
I have downloaded the files on my Desktop of my Linux machine. What to do next i.e. how to incorporate gridfit in MATLAB 2007?
smp

Grzegorz Knor
Grzegorz Knor on 29 Nov 2011
Add folder which contains this function to search path, and type:
help gridfit

smp
smp on 30 Nov 2011
I did as you said.
Then I used this command:
zgrid = gridfit(x,y,z,xi,yi)
where
x= experimental x co-ordinates
y= experimental y co-ordinates
z= experimental values for each pair (x,y)
and
xi = linspace(-0.8,0.8,50)
yi= linspace(-0.8,0.8,50)
But where and when I will provide my "model surface" to be fitted to experimental data (x,y,z(x,y))?
My model surface is z(x,y)= c*( 2*(x^2+y^2)-1 )
And more important for me : How to obtain fitted coefficient c?
Thanks & Regards,
smp

Grzegorz Knor
Grzegorz Knor on 30 Nov 2011
Gridfit produce only smooth surface that approximates your data.
If you want to calculate the coefficient c from your model look at the example:
[x y] = meshgrid(-1:.1:1);
c = sqrt(2);
z = c*( 2*(x.^2+y.^2)-1 );
z = z + randn(size(z))/5;
plot3(x,y,z,'r.')
f = @(c)norm(z-c*( 2*(x.^2+y.^2)-1));
c1 = fminsearch(f,1);
z1 = c1*( 2*(x.^2+y.^2)-1 );
hold on
surf(x,y,z1,'FaceColor','none')

smp
smp on 30 Nov 2011
I worked out your example in my machine. It worked well. But I have some problems when I tried to do similar thing to my data:
I did this:
[x,y]=meshgrid(-0.707107:0.141421:0.707107)
c=1.0
f = @(c)norm(z-c*(2*(x^2+y^2)-1))
where z is a vector of experimental values (which I had loaded earlier by command "load z.txt")
Then I gave this command:
c1=fminsearch(f,0.1)
Then I got this message:
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> @(c)norm(z-c*(2*(x^2+y^2)-1))
Error in ==> fminsearch at 205
fv(:,1) = funfcn(x,varargin{:});
So what's my mistake?
Thanks & Regards,
smp
  1 Comment
Grzegorz Knor
Grzegorz Knor on 30 Nov 2011
If z is a vector, then x and y should be vectors too.
BTW: add dots to this line before ^:
f = @(c)norm(z-c*(2*(x.^2+y.^2)-1))

Sign in to comment.


smp
smp on 1 Dec 2011
I started afresh:
>> load x.txt
>> load y.txt
>> load z.txt
Here x,y,z are vectors each containing 121 entries. Basically I have array of 11x11 x values, array of 11x11 y values, and corresponding array of 11x11 z values. But for some reason I want x,y,z in vector forms.
Then I gave these commands:
>> c=1.0
>> f = @(c)norm(z-c* (2*(x.^2+y.^2)-1 ) )
>> c1=fminsearch(f,0.1)
(why dots are needed for x & y in 'f' ?)
I got c1 = 1.4958
But when I changed c to 5, still I got c1=1.4958
Obviously this is wrong.
Thanks & Regards,
smp
  2 Comments
Walter Roberson
Walter Roberson on 1 Dec 2011
See the reference material for mpower ("^") and power (".^") to see when to use one or the other.
Changing the initial value of c to 5 has no effect on the code. The anonymous function f does not use the existing value of c in any way. The anonymous function uses c as a "dummy argument". Nothing would change if you were to instead use
f = @(ThisC)norm(z-ThisC* (2*(x.^2+y.^2)-1 ) )
smp
smp on 1 Dec 2011
Ok. thanks. But then what is the way to find fitted coefficient c?
Regards,
smp

Sign in to comment.


smp
smp on 1 Dec 2011
To Walter Roberson (regarding your comment): Ok. thanks. But then what is the way to find fitted coefficient c?
Regards, smp

Community Treasure Hunt

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

Start Hunting!