views:

61

answers:

1
load X_Q2.data
load T_Q2.data
x = X_Q2(:,1);
y = X_Q2(:,2);

learningrate = 0.2;
max_iteration = 50;

% initialize parameters
count = length(x);
weights = rand(1,3); % creates a 1-by-3 array with random weights
globalerror = 0;
iter = 0;
while globalerror ~= 0 && iter <= max_iteration
  iter = iter + 1;
  globalerror = 0;
  for p = 1:count
    output = calculateOutput(weights,x(p),y(p));
    localerror = T_Q2(p) - output
    weights(1)= weights(1) + learningrate *localerror*x(p);
    weights(2)= weights(1) + learningrate *localerror*y(p);
    weights(3)= weights(1) + learningrate *localerror;
    globalerror = globalerror + (localerror*localerror);
  end 
end  

I out this fuunc in some other file.

function result = calculateOutput (weights, x, y)
s = x * weights(1) + y * weights(2) + weights(3);
if s >= 0
    result = 1;
else
    result = -1;
end

Nothing is coming out. I out the code in the command window and press enter....nothing apperars on the window. How do i get the output?

+3  A: 

You can't use the variable globalerror in the condition check of your while loop because you don't define that variable as anything until within the loop. This is why you are getting the error "Undefined function or variable 'globalerror'". You have to initialize globalerror to some value before you try to use it in any statements.

Also, as I mentioned in my answer to your previous question, you can't declare functions inside scripts. Try cutting out the function calculateOutput from the above script and place it in its own file called calculateOutput.m, then save that somewhere on the MATLAB path.

And a few additional problems I see:

  • MATLAB uses 1-based indexing, not 0-based indexing. In other words, the first element of a vector or a matrix dimension is indexed by the value 1, not 0.
  • I have no idea what you are trying to do with this line:

    localerror = output(p) - output
    

    since the variable output is a scalar in your code, not a vector that can be indexed by p.

gnovice
since i have two data files, one for input and one for output. I should rather write T_Q2[p]...what do u say?
You can check the format of these 2 files in my other question....http://stackoverflow.com/questions/3445484/matlab-syntax-errors-in-single-layer-neural-network

related questions