主程式
function F=ex_crank(theta)
global rl th2;
F=[rl(2)*cos(th2)-theta(2)*cos(theta(1));
rl(1)+rl(2)*sin(th2)-theta(2)*sin(theta(1));
rl(3)*cos(theta(1))+rl(4)*cos(theta(3))-theta(4);
rl(3)*sin(theta(1))+rl(4)*sin(theta(3))-rl(5);]
end
副程式
clear all;
global rl th2;
rl=[2.9,1.2,1.9,3.5,6,0.95];
rowlen=360;
thetaSol=zeros(rowlen+1,4);
couplerP=zeros(rowlen+1,4);
theta0=[0*pi/180;180*pi/180];
for i=0:rowlen
th2=i*pi/180;
options=optimset('Display','iter');
[theta,fval]=fsolve(@ex_crank,theta0,options);
thetaSol(i+1,:)=theta';
couplerP(i+1,:)=[(rl(2)-rl(end))*cos(th2),...
rl(1)+(rl(2)-rl(end))*sin(th2)];
theta0=theta;
end
這兩段是我的程式 但一直跑不出結果
他一直顯示這個錯誤
Attempted to access theta(3); index out of bounds because numel(theta)=2.
但我不知道是哪裡出問題了??
所以想請教各位
因為
theta0=[0*pi/180;180*pi/180];
' = =初始條件只有給兩個變數,結果你在方程式內要找四個變數??第三第四個要去哪邊生......
而且
這個應該才叫副程式!!!!
function F=ex_crank(theta)
global rl th2;
F=[rl(2)*cos(th2)-theta(2)*cos(theta(1));
rl(1)+rl(2)*sin(th2)-theta(2)*sin(theta(1));
rl(3)*cos(theta(1))+rl(4)*cos(theta(3))-theta(4);
rl(3)*sin(theta(1))+rl(4)*sin(theta(3))-rl(5);]
end
這邊才是主程式
clear all;
global rl th2;
rl=[2.9,1.2,1.9,3.5,6,0.95];
rowlen=360;
thetaSol=zeros(rowlen+1,4);
couplerP=zeros(rowlen+1,4);
theta0=[0*pi/180;180*pi/180];
for i=0:rowlen
th2=i*pi/180;
options=optimset('Display','iter');
[theta,fval]=fsolve(@ex_crank,theta0,options);
thetaSol(i+1,:)=theta';
couplerP(i+1,:)=[(rl(2)-rl(end))*cos(th2),...
rl(1)+(rl(2)-rl(end))*sin(th2)];
theta0=theta;
end
fsolve(要解決的方程式,初始條件,參數優化設定)
希望可以為你解決