MATLAB Answers

0

a and b constant

Asked by MOHAMED YHYA on 21 Apr 2019 at 13:24
Latest activity Answered by Image Analyst
on 21 Apr 2019 at 17:11
HI, im new to matlab and was wondering how would you input a command to help me solve an unknown constant in an equation.
I have a bunch of x,y points,The equation would be x= a*y+b

  0 Comments

Sign in to comment.

2 Answers

Answer by Image Analyst
on 21 Apr 2019 at 17:11
 Accepted Answer

You say "The equation would be x= a*y+b" and this is the opposite to the way people usually do things, but it can be done. Just reverse x and y when you call polyfit(). Try this:
coefficients = polyfit(y, x, 1); % x as a function of y - note NOT the usual way people do things!
a = coefficients(1)
b = coefficients(2)

  0 Comments

Sign in to comment.


Answer by Stephan
on 21 Apr 2019 at 13:52
Edited by Stephan
on 21 Apr 2019 at 13:56

% Only needed if x,y are not column vectors
x = reshape(x,[],1);
y = reshape(y,[],1);
% solve for a, b
x = [x, ones(numel(x),1)];
sol = y\x;
a = sol(1);
b = sol(2);

  0 Comments

Sign in to comment.