Hi, I have a problem looping a piece of code, I get weird errors from using while or I'm making a mistake, I don't know at this time, but I could really use a hand.
Basically, my program calculates functions f and g, which are function of x,y and m; m is fixed at 0.25 and I need to find the values of x and y for which functions f and g near zero.
Here's the code:
x=0; y=1; m=0.25; tol=0.05; f=0; g=0; %values for x and y are just for initialization, m remains 0.25 %throughout; I chose a tolerance of 0.05, but it could be different; %functions f and g are initialized as null
f=m-y*sin(x); g=m+2*y^2-2*y*cos(x);
%functions f and g are calculated with initial values for x,y,m
dfg=[f g]';
%dfg is a column vector for f and g values
J=[-y*cos(x) -sin(x); 2*y*sin(x) 4*y-2*cos(x)];
%here the Jacobian matrix is calculated, which contains partial %differentials for functions f and g, each in relation to x and y; %J=[df/dx df/dy; dg/dx dg/dy];
dxy=(J^(-1))*dfg;
%dxy is the deviation vector, the multiple of the inverted J matrix %and the value vector for the two functions f and g
x=x+dxy(1); y=y+dxy(2);
%here x and y are adjusted by adding the corresponding values from %the deviation vector
Now, what I need the program to do is loop, as in run until the function values are smaller than the tolerance (dfg(1) and dfg(2) < tol (tol is adjustable, but should be small, near zero)). I've tried while and for and I get weird errors for them, but I could just simply be doing it wrong 🙂
Please help me and tell me how to loop this thing until f,g < tol
[code] x=0; y=1; m=0.25; tol=0.1; f=0; g=0;
f=m-y*sin(x); g=m+2*y^2-2*y*cos(x); dfg=[f g]'; J=[-y*cos(x) -sin(x); 2*y*sin(x) 4*y-2*cos(x)]; dxy=(J^(-1))*dfg; x=x+dxy(1); y=y+dxy(2);
[/code]
Best Answer