Two functions and one IF loop

2 views (last 30 days)
Suhailjeet Singh
Suhailjeet Singh on 26 Nov 2015
Hi I have following code in which I want to solve the 3 nonlinear equation using fsolve. My result should give me the 3 angles 't' 'u' and 'p'. I have 3 equations given by fcns(1),fcns(2) and fcns(3) in which pwx,pwy and pwz values are defined and P(1,1) P(2,1) P(3,1) is got from T03. I think im not able to sequnce the function properly. Can someone kindly help The name of the m file is parterough.m
k=3;x=sym('x');y=sym('y');z=sym('z');t=sym('t');u=sym('u');p=sym('p');
a=[2 0 0 t;3 0 0 u;4 0 0 p];pwx=5;pwy=6;pwz=0;
for i=1:k
A{i}=[cos(a(i,4)) -sin(a(i,4))*cos(a(i,2)) sin(a(i,2))*sin(a(i,4)) a(i,1)*cos(a(i,4));
sin(a(i,4)) cos(a(i,4))*cos(a(i,2)) -cos(a(i,4))*sin(a(i,2)) a(i,1)*sin(a(i,4));
0 sin(a(i,2)) cos(a(i,2)) a(i,3);0 0 0 1];
end
A01=A{1};
A12=A{2};
A23=A{3};
if k==3
T03=A01*A12*A23
P=T03(1:4,4:4)
else
A34=A{4};
T04=A01*A12*A23*A34
P=T04(1:4,4:4)
function parterough()
guess=[0 0 0];
result=fsolve(@eqns,guess);
result=result*(180/pi)
end
function fcns=eqns(z)
t=z(1);
u=z(2);
p=z(3)
fcns(1)=pwx-P(1,1);
fcns(2)=pwy-P(2,1);
fcns(3)=pwz-P(3,1);
end
  1 Comment
Walter Roberson
Walter Roberson on 26 Nov 2015
There is no such thing as an "if loop". There is "for loop", there is "while loop", and there is "if statement".

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 26 Nov 2015
You are missing the "end" statement for "if k==3" and for "for i = 1:k"
Your "if" is testing "k==3" but you know ahead of time that it is 3. Did you mean "if i==3" ?
You have defined two functions but you do not use the functions.
Your function parterough calculates a value but does not return it. Are you counting on the value being printed out on the display?

Suhailjeet Singh
Suhailjeet Singh on 26 Nov 2015
No I did not mean i==3 since the value of K will be taken from the user. Im gonna use it in GUI and take the input for k from the user. Im just testing this if it works. Also i want to know how to sequence the two functions
function parterough() guess=[0 0 0]; result=fsolve(@eqns,guess); result=result*(180/pi)
k=3;x=sym('x');y=sym('y');z=sym('z');t=sym('t');u=sym('u');p=sym('p'); a=[2 0 0 t;3 0 0 u;4 0 0 p];pwx=5;pwy=6;pwz=0; for i=1:k A{i}=[cos(a(i,4)) -sin(a(i,4))*cos(a(i,2)) sin(a(i,2))*sin(a(i,4)) a(i,1)*cos(a(i,4)); sin(a(i,4)) cos(a(i,4))*cos(a(i,2)) -cos(a(i,4))*sin(a(i,2)) a(i,1)*sin(a(i,4)); 0 sin(a(i,2)) cos(a(i,2)) a(i,3);0 0 0 1]; end A01=A{1}; A12=A{2}; A23=A{3}; if k==3 T03=A01*A12*A23 P=T03(1:4,4:4) else A34=A{4}; T04=A01*A12*A23*A34 P=T04(1:4,4:4) end function fcns=eqns(z) t=z(1); u=z(2); p=z(3) fcns(1)=pwx-P(1,1); fcns(2)=pwy-P(2,1); fcns(3)=pwz-P(3,1); end
even this doesn't seem to work!!!

Community Treasure Hunt

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

Start Hunting!