Quantcast
Channel: MATLAB Central Newsreader - tag:"optimization"
Viewing all articles
Browse latest Browse all 130

Re: Estimation of variables to minimize the sum of residuals

$
0
0
Hello Rakesh,
This post had been for a while so you might not check this reply or not but it seems that your answer for Prakhar is very clear and helpful. I ran into the similar problem with Prakhar and found this post while I was googling.
I'm not familiar with the MATLAB but trying to figure out how to use this program.
When I looked at your code, I intuitively understood.
But when I was trying to manipulate it, it made an error.

First, when I put k=[a b c], the MATLAB said 'undefined function or variable 'a'.
Do I need to define a,b,c?

Second, the function and the lsqcurvefit function make errors as well.

Is this because the version has been changed to 2016?

Hope you see this reply.

Regards,
Ryan

"Rakesh Kumar" wrote in message <k7b8ag$4jn$1@newscl01ah.mathworks.com>...
> "Prakhar " <prakhar_cool@yahoo.com> wrote in message <k79nrs$oss$1@newscl01ah.mathworks.com>...
> > Hi everyone
> >
> > Here is the problem which I am working on:
> > ***********************************************
> > Let's say we have an array x = [1; 2; 3; 4]. Now, lets define a function f = (a + b*y + c*y.^2) where y is an array with same size as x. (e.g: y = [-2; -1; 0; 2])
> >
> > Define Residual:
> >
> > res = x - f
> > (e.g.: res(1) = x(1) - (a + b*y(1) + c*y(1)^2).
> >
> > Define sum of square of residuals as:
> >
> > s = sum(res.^2).
> >
> > Now, i need to find variables a, b, c in such a way so that the sum of residuals 's' is minimum.
> > *****************************************************
> > My approach:
> >
> > I defined residual as an inline function
> >
> > x = [1; 2; 3; 4]
> > y = [-2; -1; 0; 2]
> > res = inline('x - (a + b*y + c*y.^2), 'x', 'y', 'a', 'b', 'c');
> > var = @(var) f(x, y, var(1), var(2), var(3));
> > *****************************
> >
> > What i am not able to code is how to estimate the sum of the residual and then how to find values of a, b, c such that this sum is minimum. I know that there is a function called fminsearch...but i am not able to apply this here. Any help with be appreciated
> >
> > Thanks a lot!
> >
> > Regards
> > Prakhar (PhD student, Caltech)
>
> Easiest way is to use lsqcurvefit function in optimization toolbox. I am pretty sure Caltech have license for optimization toolbox.
>
> Here is what I did on your test example. Assume, k = [a b c]
> y = [-2; -1; 0; 2]; % typically called 'xdata' in lsqcurvefit help pages
> x = [1; 2; 3; 4]; % typically called 'ydata'
> % define model
> fun = @(k,y) k(1) + k(2)*y + k(3)*y.^2
> % Fit model by minimizing the least square of the residual
> [o1,o2,o3] = lsqcurvefit(fun,[1 2 3], y, x)
>
> Look at the example at the bottom of this page:
> http://www.mathworks.com/help/optim/ug/lsqcurvefit.html

Viewing all articles
Browse latest Browse all 130

Trending Articles