0
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
  • As a community, would this question be better if code were edited down to: while x ~= 0 x = 10; x = x - func(x) end It is more concise for future readers and has the same concepts. Commented Aug 10, 2010 at 19:51
  • @MatlabDoug: That would help future readers who might have similar problems to those asked by the OP, but there are actually some other problems specific to this code that the OP didn't yet realize were problems. I would say it should be left as is to help this specific user with those additional errors. The title edits (and perhaps some tag editing) should help generalize the question. Commented Aug 10, 2010 at 20:09
  • @ishamahajan: One problem with your updated code is that the while loop is never entered because you initialized globalerror to 0. You would have to initialize it to a non-zero value. However, this is the least of your problems. It seems that you are having a great deal of difficulty even understanding the algorithms involved, let alone how you might translate them from another language into MATLAB. I think you need to take some time to learn the basics of MATLAB, starting with the online documentation, which is quite good. Commented Aug 11, 2010 at 5:25

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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....stackoverflow.com/questions/3445484/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.