I have data point which fits the line y=mx+c ? How can I write code for this?
Show older comments
I have data point which fits the line y=mx+c ? How can I write code for this?
Answers (2)
sixwwwwww
on 7 Dec 2013
write as follow
x = 0:100;
m = 3;
c = 4;
y = m * x + c;
plot(x, y)
7 Comments
RS
on 7 Dec 2013
sixwwwwww
on 7 Dec 2013
then use curve fitting tool. see
doc cftool
if you have x and y given then use directly
cftool(x, y)
RS
on 7 Dec 2013
sixwwwwww
on 7 Dec 2013
if values of x and y are given then you can do as follow. I suppose you x and y vectors then:
m = (y(2) - y(1)) / (x(2) - x(1));
c = x(1) - m * y(1);
RS
on 7 Dec 2013
sixwwwwww
on 7 Dec 2013
The problem is that in order to plot the equation you need to have 3 known values and one unknown value because there 4 values in equation x, y, m and c. So you should atleast have three values or if you just plot some random values of x and y and then want to find m and c then using cftool is the best option
If you are given values of x and y then you can compute m and c as follow:
fitvars = polyfit(x, y, 1);
m = fitvars(1);
c = fitvars(2);
chitresh
on 7 Dec 2013
1 vote
>>syms x y m c
>> solve(' y = m * x + c')
i think this gone solve your question, if it is solved accept the answer
4 Comments
RS
on 8 Dec 2013
chitresh
on 13 Dec 2013
dear here m and c where constant, by substituting the value of x and y you get your answer and you can plot your lines too for this plz go through linear programming mathematics here is a link for this
chitresh
on 15 Dec 2013
if you done with your question, mark the answer accepted
Image Analyst
on 15 Dec 2013
And officially Accept answers in more than 30 other questions that you've asked.
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!