Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fitting a curve

Subject: fitting a curve

From: amarender

Date: 3 Mar, 2009 08:07:02

Message: 1 of 11

hi,
  I have 3 set of data points without any mathematical relation b/w them.Can i fit curve passing through those points by using MATLAB,or should i find any other software???

Subject: fitting a curve

From: Miroslav Balda

Date: 3 Mar, 2009 10:05:03

Message: 2 of 11

"amarender " <files@mathworks.com> wrote in message <goiof6$qk0$1@fred.mathworks.com>...
> hi,
> I have 3 set of data points without any mathematical relation b/w them.Can i fit curve passing through those points by using MATLAB,or should i find any other software???

Yes, Matlab is able to do a lot. However, if you want to receive a better answer, you have to formulate a better question.

Mira

Subject: fitting a curve

From: John D'Errico

Date: 3 Mar, 2009 10:53:00

Message: 3 of 11

"amarender " <files@mathworks.com> wrote in message <goiof6$qk0$1@fred.mathworks.com>...
> hi,
> I have 3 set of data points without any mathematical relation b/w them.Can i fit curve passing through those points by using MATLAB,or should i find any other software???

If you have only 3 points, then expect very little in
terms of a quality result. A straight line, or at most,
a quadratic polynomial, is the best you can do.

If you have more than 3 points, but have merely
worded your question very poorly, then expect
very little help until you explain what your problem
is more clearly.

John

Subject: fitting a curve

From: amarender

Date: 5 Mar, 2009 04:14:02

Message: 4 of 11

"Miroslav Balda" <balda.remove.this@cdm.cas.cz> wrote in message <goivcf$f4c$1@fred.mathworks.com>...
> "amarender " <files@mathworks.com> wrote in message <goiof6$qk0$1@fred.mathworks.com>...
> > hi,
> > I have 3 set of data points without any mathematical relation b/w them.Can i fit curve passing through those points by using MATLAB,or should i find any other software???
>
> Yes, Matlab is able to do a lot. However, if you want to receive a better answer, you have to formulate a better question.
>
> Mira
Hi,
I have x=(0.45,0.98,0.34-------------------------------- 50 values)
         y=(3.4,0.008,---------------------------------------- 50 values)
         z=(0.6,0.23,-------------------------------------------50 values)
                and I want to plot 3d curve through those points,Can I do that with MATLAB or Can I do it with any other software(I don't have any mathematical relation b/w x,y,z)??????)

Subject: fitting a curve

From: Steven Lord

Date: 5 Mar, 2009 15:03:08

Message: 5 of 11


"amarender " <files@mathworks.com> wrote in message
news:gonjia$hep$1@fred.mathworks.com...
> "Miroslav Balda" <balda.remove.this@cdm.cas.cz> wrote in message
> <goivcf$f4c$1@fred.mathworks.com>...
>> "amarender " <files@mathworks.com> wrote in message
>> <goiof6$qk0$1@fred.mathworks.com>...
>> > hi,
>> > I have 3 set of data points without any mathematical relation b/w
>> > them.Can i fit curve passing through those points by using MATLAB,or
>> > should i find any other software???
>>
>> Yes, Matlab is able to do a lot. However, if you want to receive a better
>> answer, you have to formulate a better question.
>>
>> Mira
> Hi,
> I have x=(0.45,0.98,0.34-------------------------------- 50 values)
> y=(3.4,0.008,---------------------------------------- 50 values)
> z=(0.6,0.23,-------------------------------------------50 values)
> and I want to plot 3d curve through those points,Can I do
> that with MATLAB or Can I do it with any other software(I don't have any
> mathematical relation b/w x,y,z)??????)

Do you want to find a mathematical relationship between the data and plot
that mathematical relationship or do you want to just plot the data? Your
first message indicates the former, your second message indicates the
latter.

If you just want to plot the data, look at the help for the PLOT3 function.

If you want to generate a mathematical relationship between the data, search
in the documentation for "regression". You can bring up the documentation
using the DOC command. Note that you will need to offer at least some
information about the type of relationship you want to fit -- MATLAB will
not "automatically guess that your data is best fit by a*x+b*y", for
instance.

--
Steve Lord
slord@mathworks.com

Subject: fitting a curve

From: Miroslav Balda

Date: 5 Mar, 2009 21:47:01

Message: 6 of 11

"Steven Lord" <slord@mathworks.com> wrote in message <goopjc$79t$1@fred.mathworks.com>...
SNIP
> If you just want to plot the data, look at the help for the PLOT3 function.
>
> If you want to generate a mathematical relationship between the data, search
> in the documentation for "regression". You can bring up the documentation
> using the DOC command. Note that you will need to offer at least some
> information about the type of relationship you want to fit -- MATLAB will
> not "automatically guess that your data is best fit by a*x+b*y", for
> instance.
>
> --
> Steve Lord
> slord@mathworks.com
>
Hi
1. I should use plot3(x,y,z) at first to see how the function looks out (see help plot3).
2. It is necessary to choose an appropriate function f(x,y,z)=0, which would have a chance to come near the points as much as possible.
3. The next steps depend on your problem. If x and y are fixed, and z is the function of both, your function would be of the form f(x,y) - z = res, where res is a vector of equation residuals. Then, you may use any of the lest squares procedure to solve this set of (in general nonlinear) equations. If your problem is different, you have to use a differnt approach as well.
Hope it helps.

Mira

Subject: fitting a curve

From: amarender

Date: 6 Mar, 2009 10:36:01

Message: 7 of 11

Hi,thanks for giving response,I have
X=(5.377292075
4.92604722
2.250950211
4.329977143
4.254508568
5.758103385
6.930152596
6.748974992
4.131837484
6.491626914
6.890396431
8.15444975
5.464671717
6.795234058
10.09036705
50.30489182
0.391117449
0.782234898
1.564469796
2.033810734
5.446722734
2.579933907
3.309706309
5.763125418
5.355289487
5.013868381
55.46248941
2.033810734
7.863895535
6.314413763
7.154686201
7.905777481
8.243983982
7.480225104
6.161895571
4.717374193
4.260290647
4.70159455
4.93942712
2.57445418
2.787282386
3.721707046
3.31418682
3.272393886
3.537379313
2.37739355
2.035122573
2.222883727
1.254049365
1.295153711
1.488587635
0.352254508
0.424116061
0.570214444)


Y=(0.074962583
0.285998415
0.904115233
0.343225832
0.356501622
0.190426021
0.136415278
0.063254074
0.190772162
0.098854349
0.148019097
0.097483698
0.187145347
0.073036756
0
0
1
1
1
1
0.272914708
0.72796699
0.543774779
0.233912559
0.233912559
0
0
1
0.179041216
0.174085591
0.120937875
0.099998949
0.087223003
0.067393711
0.316747535
0.397620695
0.422060014
0.365823339
0.316758655
0.911633569
0.813931867
0.567267949
0.330020374
0.33441115
0.287654127
0.328541551
0.345702789
0.351922483
0.311454298
0.301725268
0.262532288
0.267008208
0.277488064
0.366804294)


Z=(0.00262251
0.384291866
99.51512886
0.654064527
0.748202861
0.100514516
0.04021992
0.00222726
0.052126459
0.01056599
0.054248144
0.01583236
0.084557754
0.003808559
0
0
23.53537162
94.14148647
376.5659459
636.3964485
0.385835992
14.51265252
3.643875604
0.228881057
0.197633068
0
0
636.3964485
0.147259681
0.085113327
0.027215804
0.016353627
0.010736741
0.003443953
0.931137422
1.517447102
1.641947083
1.026711818
0.59841772
146.9263902
44.55978476
5.852440179
0.322219375
0.332950361
0.203065045
0.162570606
0.149183598
0.192733024
0.035853012
0.033361679
0.024518995
0.001472938
0.002508145
0.015286705) and now I want to draw a 3-D curve passing through those points.

Subject: fitting a curve

From: Miroslav Balda

Date: 6 Mar, 2009 11:26:03

Message: 8 of 11

"amarender " <files@mathworks.com> wrote in message <goquah$172$1@fred.mathworks.com>...
> Hi,thanks for giving response,I have
> X=(5.377292075
> 4.92604722
:
SNIP
:
> 0.002508145
> 0.015286705) and now I want to draw a 3-D curve passing through those points.

Hi,
I am curious if you have tried to plot your data I have recommended to you. I am affraid not.
If you did it, you saw a cloud of mutualy idependent points in space. If you "want to draw a 3-D curve passing through those points", do it. I am not able and not responsible for such a task.
Mira

Subject: fitting a curve

From: Mathieu C.

Date: 6 Mar, 2009 12:14:02

Message: 9 of 11

"amarender " <files@mathworks.com> wrote
>... and now I want to draw a 3-D curve passing through those points.

See the help for griddata then.

Mathieu

Subject: fitting a curve

From: Richard Willey

Date: 6 Mar, 2009 15:56:38

Message: 10 of 11

Hi Amrender

The 2009a release of MATLAB just shipped this morning. It has some brand
new functionality that sounds like a very good match for your use case.

The 2009a release includes some major enhancements to the Curve Fitting
Toolbox product. Curve Fitting Toolbox 2.0 now supports surface fitting.
The toolbox includes a surface fitting algorithm called LOWESS (also known
as Locally Weighted Scatterplot Smoothing, aka localized regression)

LOWESS is a nonparametric algorithm. This means that you don't have to
specify any functional form for your model. So long as you have a
reasonably dense cloud of data points, you can normally generate a pretty
good fit.

LOWESS does have some limitations. In particular, you need to specify the
bandwidth for the smoother. If you're bandwidth is too large, your R^2 will
be pretty bad. If your bandwidth is too small, you'll get a great R^2, but
you'll "overfit" the data. Your best option is to use a technique called
Cross Validation to estimate a good bandwidth for the smoother.

The following code will give you a pretty good idea how to use this
functionality.

The code generates a noisy surface using the peaks function inside MATLAB.

The demo then uses cross validation to estimate a smoothing parameter and
then uses a bootstrap to estimate confidence intervals.

This technique is very similar to methods that are described in "The
Elements of Statistical Learning" by Hastie, Tibshirani, and Friedman.

(You'll need Statistics Toolbox and Curve Fitting Toolbox 2.0 to run this
code)

 %% Generate a dataset



clear all



a = 3;

b = -10;

c = -1/3;



P = haltonset( 2 );

P = scramble( P, 'RR2' );

X = net( P, 300);



x = ((X(:,1)) * 6) - 3;

y = (X(:,2) * 6) - 3;



z = a*(1-x).^2.*exp(-(x.^2) - (y+1).^2) + b*(x/5 - x.^3 -
y.^5).*exp(-x.^2-y.^2) + c*exp(-(x+1).^2 - y.^2);



% Add noise



noise = (0.05 *(range(z))*randn( size( z ) ));



z = z + noise;



%% Use Cross Validation to estimate the optimal bandwidth for the smoother



tic



nSpans = 49;

spans = log10( logspace( 0.02, 0.5, nSpans ) );



mse = zeros( size( spans ) );

cp = cvpartition( size( X, 1 ), 'k', 10 );



for j = 1:nSpans



    f = @( x, y, xi ) feval( fit( x, y, 'lowess', 'span', spans(j) ), xi );

    mse(j) = crossval( 'mse', X, z, 'PredFun', f, 'Partition', cp );



end



toc



%% Identify the span that minimizes the Mean Squared Error



[minmse, minj] = min( mse );

span = spans(minj);



semilogx( spans, mse, '-', span, minmse, 'ro' )

title( sprintf( 'Best span is %f', span ) );



%% Plot a Surface with that Span



set( gca, 'XScale', 'Linear' );

sf = fit( X, z, 'lowess', 'span', span )

plot( sf, X, z );

view( 135, 15 )



%% Richard's homemade nonparametric residual bootstrap



tic



nboot = 350;



Zhat = sf(X(:,1),X(:,2));

Residuals = z - Zhat;



dump = zeros(length(X),nboot);



for i = 1:nboot



    Eresiduals = randsample(Residuals,length(Residuals),true);

    EZhat = Zhat + Eresiduals;

    sfb = fit( X, EZhat, 'lowess', 'span', span );

    dump(:,i) = sfb(X(:,1),X(:,2));



end



stdloess = std(dump,0,2);



meanloess = mean(dump,2);

plot(Zhat(:),meanloess(:),'o')



toc



%% Generate 95% confidence intervals for the Surface



% 95% of the surfaces generated will fall between +- 2 standard deviations

% of the original surface



sf_lb = fit(X, Zhat - 2*stdloess, 'linearinterp');

sf_ub = fit(X, Zhat + 2*stdloess, 'linearinterp');



plot( sf, X, z );

plot( sf_lb, 'Parent', gca );

plot( sf_ub, 'Parent', gca );

view( 135, 15 )

Subject: fitting a curve

From: John D'Errico

Date: 6 Mar, 2009 16:05:19

Message: 11 of 11

"Richard Willey" <rwilley@mathworks.com> wrote in message <gorh3m$pdp$1@fred.mathworks.com>...
> Hi Amrender
>
> The 2009a release of MATLAB just shipped this morning. It has some brand
> new functionality that sounds like a very good match for your use case.
>
> The 2009a release includes some major enhancements to the Curve Fitting
> Toolbox product. Curve Fitting Toolbox 2.0 now supports surface fitting.

I'm glad to see it. This is long awaited.

John

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us