Population and profit using linear regression
2 次查看(过去 30 天)
显示 更早的评论
Suppose there are 97 data sets .I have to compute the cost using linear regression .I am calling the data from a txt. file .
function J = computeCost(X, y, theta)
J=0;m = length(y);
data=load('ex1data1.txt');
x=data(:,1);y=data(:,2);
h=X*theta;
error=h-y;
error_sqr=(h-y).^2;
J=(1/(2*m))*(sum(error_sqr));
If I use the following code in command window then the following results is shown.What is the problem with the code ?
computeCost(ones(m, 1), data(:,1),data(:,2),zeros(2, 1))
Undefined function or variable 'm'.
0 个评论
采纳的回答
Star Strider
2015-9-8
This line is in your main (or calling) script, and you likely have not yet defined ‘m’ in it so that ‘m’ is a variable in the main script workspace:
computeCost(ones(m, 1), data(:,1),data(:,2),zeros(2, 1))
If ‘m’ is first defined in the function, it is local to that function, not to the main script workspace.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!