!! Submission failed: unexpected error: Error using computeCost Too many output arguments.

When I put this expression in MATlab I get:
!! Submission failed: unexpected error: Error using computeCost
Too many output arguments.
!! Please try again later.
function J = computeCost(X, y, theta)
data = load('ex1data1.txt');
m = 97;
X = [ones(m, 1), data(:,1)];
theta = zeros(2, 1);
h = X * theta;
y = data(:, 2);
error = h - y;
error_sqr = error.^2;
q = sum(error_sqr);
J = (1/ (2*m) * q)

3 comentarios

Why are you overwriting the input X and y and theta?
What is the point of setting theta to 0 and then multiplying by theta ?
How can you be certain that data will always have exactly 97 rows?
Thank you for your questions.
It is my first promgramming excercise.
Many things are wrong.
Please tell me where I overwrite the inputs X, y and theta.
There is no point on setting theta to 0. My mistake.
I have no certainty of the number of rows, I tried to have a definite result. Now I understand that my programming has to fit any data.
function J = computeCost(X, y, theta)
That line tells you that X, y, and theta are normally expected to be passed in as inputs.
data = load('ex1data1.txt');
m = 97;
Neither of those lines read or write X, y, or theta, so the input X, y, theta do not matter to those two lines.
X = [ones(m, 1), data(:,1)];
theta = zeros(2, 1);
h = X * theta;
y = data(:, 2);
The first of those lines ignores any X that was passed in and overwrites X with values. The second discards theta that was passed in and overwrites theta with zeros. The fourth line discards y that was passed in and overwrites y with values.
Therefore no matter what was passed in for X, y, theta, the code will ignore them and do its own thing.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Comentada:

el 17 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by