필터 지우기
필터 지우기

Linear regression using least square method & VARARGIN

조회 수: 1 (최근 30일)
Sebastian Ciuban
Sebastian Ciuban 2013년 12월 22일
편집: Matt J 2013년 12월 22일
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')

채택된 답변

Matt J
Matt J 2013년 12월 22일
편집: Matt J 2013년 12월 22일
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.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by