Linear regression using least square method & VARARGIN

1 view (last 30 days)
Hello,
As I specified, I have to solve a linear regression using least square method. I did it in the "classical" way using numbers given by me (for parameters). After my teacher evaluated it, he recommended me to make a function that will generate random parameters using varargin..To be honest, I don't know how varargin works and I don't know how to re-do the exercise in the way that my teacher wants..Any help will be very appreciated. Thank you!
%Linear regression using least square method
x=[1:10];
y=4.*x+5;
y=y+randn(size(y)); % Measurment of y with errors (random)
A=x-mean(x);
B=A.^2;
C=(x-mean(x)).*(y-mean(y));
a=sum(C)./sum(B);
b=-mean(x).*coef1+mean(y);
y1=a*x+b;
plot(x,y1,x,y,'o')

Accepted Answer

Matt J
Matt J on 22 Dec 2013
Edited: Matt J on 22 Dec 2013
varargin allows you to pass an indefinite number of input arguments to a function. Normally, without varargin, the function signature imposes a maximum on the number of arguments that can be passed. In
function f(A,B,C)
you can pass up to 3 input arguments and they will be assigned to A, B, C in the usual way. If you try to pass 4 or more arguments in a call to f(), you will get an error. If however, the function signature is
function f(A,B,C,varargin)
then you can pass any number of arguments that you want. For example, the function call f(a,b,c,d,e) will result in the following in the workspace of f()
A=a;
B=b;
C=c;
varargin{1}=d;
varargin{2}=e;
How this applies to your assignment, however, is not at all clear.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!