Info

This question is closed. Reopen it to edit or answer.

Trying to make loop break when Xnew=Xlast, but the program stops at the wrong numbers.

3 views (last 30 days)
Hey I'm trying to make a loop that keeps going till Xnew=Xlast, but the numbers aren't coming out right. I know my formulas are correct, but something is still messing up.
If I input r0=120, r1=60, r2=140, r3=80, theta0=pi, theta1=pi/3, theta2=1, and theta3=3 I should get the numbers 0.077 and 4.044, but for some reason I'm getting 0.0005 and 5.0754. I'm pretty sure it's a code error. I'll put my code below. Can anyone help?
function [ theta2, theta3 ] = kenimaticprob( r0, r1, r2, r3, theta0, theta1 )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
r0=input('enter the length of fixed link r0: ')
r1=input('enter the length of the link r1: ')
r2=input('enter the length of coupler link r2: ')
r3=input('enter the length of driven crank link r3: ')
theta0=input('enter the value of the angle between the fixed link and the horizontal theta0: ')
theta1=input('enter the value of the angle between the crank link and the fixed link theta1: ')
theta2=input('guess the angle theta2: ')
theta3=input('guess the angle theta3: ')
f1=r2*cos(theta2)+r3*cos(theta3)+r0*cos(theta0)+r1*cos(theta1);
f2=r2*sin(theta2)+r3*sin(theta3)+r0*sin(theta0)+r1*sin(theta1);
f=[f1; f2];
g=[-r2*sin(theta2), -r3*sin(theta3) ; r2*cos(theta2), r3*cos(theta3)];
Xnew=[theta2; theta3]-inv(g)*f;
Xlast=[theta2; theta3];
Xnew=Xlast-inv(g)*f;
loop=1
Xnew=Xlast-inv(g)*f;
disp(Xnew)
Xlast=Xnew;
while(1)
f1=r2*cos(Xnew(1))+r3*cos(Xnew(2))+r0*cos(theta0)+r1*cos(theta1);
f2=r2*sin(Xnew(1))+r3*sin(Xnew(2))+r0*sin(theta0)+r1*sin(theta1);
f=[f1; f2];
G=[-r2*sin(Xnew(1)), -r3*sin(Xnew(2));r2*cos(Xnew(1)), r3*cos(Xnew(2))];
inv(G);
loop=loop+1
Xnew=Xlast-inv(g)*f;
disp(Xnew)
error_1=(Xnew(1)-Xlast(1))/Xnew(1);
error_2=(Xnew(2)-Xlast(2))/Xnew(2);
error_max=max(error_1,error_2);
es=1*10^(-4);
if(error_max>es)
Xlast=Xnew;
else
Xnew;
break
end
end

Answers (1)

Walter Roberson
Walter Roberson on 9 May 2013
  1 Comment
Image Analyst
Image Analyst on 9 May 2013
He isn't comparing for equality, despite what his subject line says, his code is checking if they're within a tolerance (a ratio of 1e-4). I wonder if either his "true" values aren't really the true values, or if his equations are wrong. By the way, he should be checking error_max=max(abs(error_1),abs(error_2)); just in case the error(s) might ever be negative.

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!