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?
You can’t use the variable
globalerrorin 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 initializeglobalerrorto 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
calculateOutputfrom the above script and place it in its own file calledcalculateOutput.m, then save that somewhere on the MATLAB path.And a few additional problems I see:
I have no idea what you are trying to do with this line:
since the variable
outputis a scalar in your code, not a vector that can be indexed byp.