Two functions and one IF loop
2 views (last 30 days)
Show older comments
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
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".
Answers (2)
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?
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!