My while loop won't execute at all

clear; clc; close all;
tol = input('Error tolerance: ');
x0 = input('Initial guess for x: ');
y0 = input('Initial guess for y: ');
z0 = input('Initial guess for z: ');
max_it = input('Max number of iterations: ');
syms x y z
J(1,:) = [diff(Function3(x,y,z),x) diff(Function3(x,y,z),y)...
diff(Function3(x,y,z),z)];
J(2,:) = [diff(Function4(x,y,z),x) diff(Function4(x,y,z),y)...
diff(Function4(x,y,z),z)];
J(3,:) = [diff(Function5(x,y,z),x) diff(Function5(x,y,z),y)...
diff(Function5(x,y,z),z)];
it = 0;
x = 0;
y = 0;
z = 0;
while Function3(x,y,z)>tol || Function4(x,y,z)>tol || Function5(x,y,z)>tol...
&& it < max_it
if it == 0
XYZ = [x0;y0;z0] - subs(inv(J),[x,y,z],[x0,y0,z0])...
*[Function3(x0,y0,z0);Function4(x0,y0,z0);Function5(x0,y0,z0)];
x = XYZ(1,1);
y = XYZ(2,1);
z = XYZ(3,1);
else
XYZ = [x;y;z] - subs(inv(J),[x,y,z],[x,y,z])...
*[Function3(x,y,z);Function4(x,y,z);Function5(x,y,z)];
x = XYZ(1,1);
y = XYZ(2,1);
z = XYZ(3,1);
end
it = it+1;
end
where the functions are each in their own separate .m file of course:
function f = Function3(x,y,z)
f = sin(2*x + y) + 2*z -1;
function f = Function4(x,y,z)
f = cos(x - y) + 3*z*x^2 - 1;
function f = Function5(x,y,z)
f = x^2 + y^2 - z*y - 2;
The point of the code is an iterative method to find the roots of the system of equations. J refers to the jacobian matrix. Basically I'm trying to find the symbolic matrix for J, and then use subs to evaluate it inside the while loop.
The issue is that it = 0 after running, and XYZ doesn't exist, so the loop isn't running at all. Is it a problem with the conditional statements? I want it to run while the iteration count is less than the max, and if any of the function values is greater than the input tolerance.
Help?!?!
Update: I changed the conditional statement and the loop is now iterating. However, it's not defining XYZ or changing the x y z values. It's looping without doing anything.
while it < max_it
while Function3(x,y,z)>tol || Function4(x,y,z)>tol || Function5(x,y,z)>tol
if it == 0
XYZ = [x0;y0;z0] - subs(inv(J),[x,y,z],[x0,y0,z0])...
*[Function3(x0,y0,z0);Function4(x0,y0,z0);Function5(x0,y0,z0)];
x = XYZ(1,1);
y = XYZ(2,1);
z = XYZ(3,1);
else
XYZ = [x;y;z] - subs(inv(J),[x,y,z],[x,y,z])...
*[Function3(x,y,z);Function4(x,y,z);Function5(x,y,z)];
x = XYZ(1,1);
y = XYZ(2,1);
z = XYZ(3,1);
end
end
it = it +1;
end

댓글 수: 4

dpb
dpb 2017년 2월 19일
Problems like this are generally one of a logic flaw and using the debugger to step through the code will generally allow you to spot the AHA! spot pretty quickly. Give that a go and then report back on results if nirvana doesn't occur...
John Birkett
John Birkett 2017년 2월 21일
Turns out sometimes you just forget an abs()... that's what did it by the way. Initial conditions caused the functions to be negative... i.e. less than the positive tolerance set. thanks!
??? Undefined function or method 'table' for input arguments of type 'double'.
Error in ==> featureExtractionForFruitImages at 29 features=table(features); what is the meaning of this error
DataSet=dir('C:\Users\farah\Desktop\classification\farah'); DBSize=length(DataSet); features=zeros(1,59); %%%% initizalizing features extraction array lables={}; % intitilaizing lables cell for text type storege k=1; for i=1:DBSize class=strcat('C:Users\farah\Desktop\classification\farah\DataSets\FIDS30\7 classess\',DataSet(i).name,'\*.jpg'); folder=dir(class); folderSize=length(folder); for j=1:folderSize %%% try and catch is for to ignore the error of reading cmyk color %%% space try img=imread(strcat('C:\Users\farah\Desktop\classification\farah\DataSets\FIDS30\7 classess\',DataSet(i).name,'\',folder(j).name)); catch continue; end
[r,c,p]=size(img);
%%%%%%condition is for to ignore the to gray error
if p==3
img=rgb2gray(img);
end
features(k,:)=extractLBPFeatures(img);
lables{k}=DataSet(i).name;
k=k+1;
end
end
features=table(features);
labled=table('lables');
features=[features labled];
Rik
Rik 2017년 3월 21일
편집: Rik 2017년 3월 21일
You are changing it outside of your while-loop, so your code keeps repeating the it==0 part. You would have noticed this if you went through your code with the debugger step by step.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 2월 19일

편집:

Rik
2017년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by