主程式
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.

但我不知道是哪裡出問題了??
所以想請教各位
幫你谷歌一下

嘗試存取theta(3)索引的界限,因為numel( theta)= 2。

拍謝獻醜了....
簡單說起來就是你在產生變數theda時,陣列只有兩個.....
因為
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(要解決的方程式,初始條件,參數優化設定)

希望可以為你解決
剛剛系統出問題結果發了兩篇,
那第二篇我在修改一下,
看起來你應該要計算的是 0, 90, 180, 270
這四個角度吧!!

theta0=[0*pi/180;180*pi/180];
改成
theta0=[0, pi/2, pi, 1.5*pi];

程式應該就可以正常勒!!!!
謝謝
已經解決了 太棒了

可以再請教你一個問題嗎??

就是怎麼用matlab 畫動畫
文章分享
評分
評分
複製連結

今日熱門文章 網友點擊推薦!