Prev: Enlarge Stateflow and multiple selection
Next: how to use textscan in a for loop with header lines increasing
From: pedro garcia on 6 May 2010 21:13 i need to create a function using [m,b]=bestfit_PG(x,y) this function finds the best fit line foe a set of data set x and y and returns the slope m and the y-intercept b of the line. this function should work for all values of x and y. x=[ 2 4 6 8 10]; y=[ 3 5 7 9 11]; FUNCTION [m,b]=bestfit_PG(x,y) if x=i m=(length(y)-y)/(length(x)-x) I REALY TRIED IT ALSO LINEAR REGRESSION TO FIND SLOPE AND YINTERSEPT THE PROBLEM IS THAT I DONT KNOW HOW TO INTERPRET THE EQUATION FOR THE SLOPE AND YINTERCEPT IN MATLAB CODE
From: Walter Roberson on 6 May 2010 22:10
pedro garcia wrote: > i need to create a function using [m,b]=bestfit_PG(x,y) this function > finds the best fit > line foe a set of data set x and y and returns the slope m and the > y-intercept b of the line. this function should work for all values of x > and y. > > x=[ 2 4 6 8 10]; > y=[ 3 5 7 9 11]; > FUNCTION [m,b]=bestfit_PG(x,y) > > if x=i > m=(length(y)-y)/(length(x)-x) > > I REALY TRIED IT ALSO LINEAR REGRESSION TO FIND SLOPE AND YINTERSEPT THE > PROBLEM IS THAT I DONT KNOW HOW TO INTERPRET THE EQUATION FOR THE SLOPE > AND YINTERCEPT IN MATLAB CODE What you show would not even pass the parser, as assignments are not allowed in an 'if' statement. You also do not have 'i' defined, so 'i' would have its default value, which is the square root of negative 1. What were you intending there? I must admit that I am having difficulty figuring out why you are using the length of the vectors in calculating the slope? |