Parameterizing function's function with a vector

1 回表示 (過去 30 日間)
Michael Kollmann 036893899
Michael Kollmann 036893899 2019 年 1 月 23 日
Hi,
(I hope I got the title indecative enough)
I'm trying to solve a system of non-linear equations in a loop using fsolve, with changing solution of the system in each loop step. My current code follows. The main issue is the function input to fsolve - @ForwardKinematics1 - takes only one vector. I wish somehow to add a second input to it, in addition to the "main" vector, so the solution to @ForwardKinematics1 would more flexible. Meaning I wish to achieve sopmething like this
fsolve(@ForwardKinematics1-parameter, input vector for @ForwardKinematics1)
This is given more explicitly below. Thanks a lot in advance
A=permn(linspace(0,2*pi,5),3);
b=[500 0 500; 700 500 400; 700 500 600];
j=1;
k=0;
for i=1:length(A)
[a, fval]=fsolve(@ForwardKinematics1,A(i,:));
if min(a<2*pi-tol) && min(a>=0) && max(abs(fval))<tol
b(j,:)=a;
j=j+1;
else
k=k+1;
end
end
and
function P=ForwardKinematics1(T)
c1=cos(T(1)); s1=sin(T(1));
c2=cos(T(2)); s2=sin(T(2));
c23=cos(T(2)+T(3)); s23=sin(T(2)+T(3));
l1=330;
l2=88;
l3=400;
l4=40;
l5=405;
A=(l2+l5*c23+l4*s23+l3*s2);
x=c1.*A;
y=s1.*A;
z=l1+l4*c23-l5*s23+l3*c2;
P=[x y z]-[500 0 500];
At the current code, ForwardKinematics1 is solved for b (1,:) -> see last line where P=[x y z]-[500 0 500];. This works fine.
What I wish to do , is to change the above to something of the following (note that now ForwardKinematics1 recieves two inputs and I added another loop so to solve ForwardKinematics1 each time for a different value). Meaning, instead of manually putting in data, demanding the solution to @ForwardKinematics1 to be equal to a certain value, I want this value to be a parameter.
A=permn(linspace(0,2*pi,5),3);
b=[500 0 500; 700 500 400; 700 500 600];
j=1;
k=0;
for l=1:length(b)
for i=1:length(A)
[a, fval]=fsolve(@ForwardKinematics1,[A(i,:),b(l,:)]);
if min(a<2*pi-tol) && min(a>=0) && max(abs(fval))<tol
b(j,:)=a;
j=j+1;
else
k=k+1;
end
end
end
and
function P=ForwardKinematics1(T,b)
c1=cos(T(1)); s1=sin(T(1));
c2=cos(T(2)); s2=sin(T(2));
c23=cos(T(2)+T(3)); s23=sin(T(2)+T(3));
l1=330;
l2=88;
l3=400;
l4=40;
l5=405;
A=(l2+l5*c23+l4*s23+l3*s2);
x=c1.*A;
y=s1.*A;
z=l1+l4*c23-l5*s23+l3*c2;
P=[x y z]-b;
However, this does not work unfortunately. Does anyone please have an idea how to do this?
Thanks again

回答 (1 件)

Steven Lord
Steven Lord 2019 年 1 月 23 日
Use one of the techniques shown on this documentation page.
  1 件のコメント
Michael Kollmann 036893899
Michael Kollmann 036893899 2019 年 1 月 23 日
Hi Steven, thanks a lot, I got to it too myself. Problem solved ;)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by