MATLAB代码无法运行

MATLAB代码无法运行,

img


function main
%输入已知数据
clear;
i1=75;
i2=255;
i3=158.78;
i4=257.01;
omega1=250;
alpha1=0; 
hd=pi/180;
%一弧度
du=180/pi;
%2 调用子函数求出铰链的机构位移,角速度,角加速度

for n1=1:361
    theta1=(n1-1)*hd; %0 到 360°
    [theta,omega,alpha]=crank_cocker(theta1,omega1,alpha1,i1,i2,i3,i4);%返回的是一个确定的值的矩阵
    theta2(n1)=theta(1);theta3(n1)=theta(2);
    omega2(n1)=omega(1);omega3(n1)=omega(2);
    alpha2(n1)=alpha(1);alpha3(n1)=alpha(2);
end
%3 角位移,角速度,角加速度的图形输出
figure(1);%figure 是建立图形的意思。系统自动从 1,2,3,4 来建立图形,数字代表第几幅图形
n1=1:361;%建立一个行向量
subplot(2,2,1);%绘制位移线图
plot(n1,theta2*du,n1,theta3*du,'k');
title('角位移线图')%设置图形标题为。
xlabel('曲柄转角、theta_1\circ')%设置 x 轴标签
ylabel('角位移/\circ')
grid on ;%显示坐标轴网格线,grid off 则关闭坐标轴网格线
hold on;%hold on 是当前轴及图像保持而不被刷新,准备接受此后将绘制的图形,多图共存。hold off(默认)则相反
text(140,170,'\theta_3') %表示在坐标 (140,170) 处添加文本'\theta_3'
text(140,30 ,'\theta_2')
%绘制角速度线图
subplot(2,2,2);
plot(n1,omega2,n1,omega3,'k')
title('角速度线图')
xlabel('曲柄转角、theta_1\circ')
ylabel('角速度/rad\cdots^{-1}')
grid on;hold on;
text(250,130,'\omega_2')
text(130,165,'\omega_3')
%角加速度线图
subplot(2,2,3);
plot(n1,alpha2,n1,alpha3,'k')
title('角加速度线图');
xlabel('曲柄转角、theta_1/\circ')
xlabel('角加速度/rad\cdots^{-2}')
grid on;hold on;
text(230,2e4,'\alpha_2')
text(30,7e4,'\alpha_3')
%铰链四杆机构图形输出
subplot(2,2,4);
x(1)=0;y(1)=0;
x(2)=i1*cos(70*hd);y(2)=i1*sin(70*hd);
x(3)=i4+i3*cos(theta3(71));y(3)=i3*sin(theta3(71));
x(4)=i4;y(4)=0;
x(5)=0;y(5)=0;
grid on;hold on;
plot(x,y)
plot(x(1),y(1),'o')
plot(x(2),y(2),'o')
plot(x(3),y(3),'o')
plot(x(4),y(4),'o')%描点,点的格式是小圆
title('铰链四杆机构')
xlabel('mm');
axis([-50 350 -20 200]);
%gtext(‘sinx’)
%4 铰链四杆机构运动仿真
figure(2)
%创建电影动画的开始
m=moviein(20)%这个函数在 2014 版本之后已经无效了
j=0;
for n1=1:5:360
    j=j+1;
    clf;
    x(1)=0;
    y(1)=0;
    x(2)=i1*cos((n1-1)*hd);
    y(2)=i1*sin((n1-1)*hd);
    x(3)=i4+i3*cos(theta3(n1));
    y(3)=i3*sin(theta3(n1));
    x(4)=i4;
    y(4)=0;
    x(5)=0;
    y(5)=0;
    plot(x,y);
    grid on;
    hold on;
    plot(x(1),y(1),'o');
    plot(x(2),y(2),'o');
    plot(x(3),y(3),'o');
    plot(x(4),y(4),'o');
    axis([-200 350 -150 200]);%axis([xmin xmax ymin   ymax]) [ ] 中分别给出 x 轴和 y 轴的最大值、最小值
    title('铰链四杆机构');
    xlabel('mm');ylabel('mm');
    %以上都是用来生成图形的,画 n1=X 时的图形
    m(j)=getframe;
                    %{该函数格式有:
                    % (1)F=gefframe,从当前图形框中得到动画帧
                    % (2)F=gefframe(h),从图形句柄 h 中得到动画帧
                    % (3)F=getframe(h,rect),从图形句柄 h 的指定区域 rec 中得到动画帧 
end
movie(m,2);
        % 该函数的主要格式有:
        % (1)movie(M),将矩阵 M 中的动画帧播放一次
        % (2)movie(M,n),将矩阵 M 中的动画帧播放 n 次
        % (3)movie(M,n,fps),将矩阵 M 中的动画帧以每秒 fps 帧的速度播放 n 次
    %movie2avi()
end
%创建动画电影的步骤:
%001——》调用 moviein 函数对内存进行初始化(该步骤在 Matlab5.3 以上均可省略),
%         创建一个足够大的矩阵,使之能够容纳基于当前坐标轴大小的一系列指定的图形(此处称为帧)。
%002——》%使用 getframe 调用 getframe 函数生成每个帧。该函数返回一个列矢量,利用这个矢量,
         %就可以创建一个电影动画矩阵.
%003——》调用 movie 函数按照指定的速度和次数运行该电影动画。
%004——》调用 movie2avi 函数可以将矩阵中的一系列动画帧转换成视频文件 avi 文件。
%         这样,即使脱离了 matlab 环境都可以播放动画。

function[theta,omega,alpha]=crank_cocker(theta1,omega1,~,i1,i2,i3,i4)
%1. 计算从动件角位移
L=sqrt(i4*i4+i1*i1-2*i1*i4*cos(theta1));
phi=asin((i1./L)*sin(theta1));
beta=acos((L*L+i3*i3-i2*i2)/(2*i3*L));
if beta<0
    beta=beta+pi;
end
theta3=pi-phi-beta;%theta3 是杆 3 转过的角度
theta2=asin((i3*sin(theta3)-i1*sin(theta1))/i2);%theta2 是杆 2 转过的角度
theta=[theta2;theta3];

%2. 计算从动件的角速度
A=[-i2*sin(theta2),i3*sin(theta3);
    i2*cos(theta2),-i3*cos(theta3)];%机构从动件位置参数矩阵
B=[i1*sin(theta1);-i1*cos(theta1)];  %原动件位置参数矩阵
omega=A\(omega1*B);
omega2=omega(1);omega3=omega(2);
%3. 计算从动件的角加速度
A=[-i2*sin(theta2),i3*sin(theta3);
   i2*cos(theta2),-i3*cos(theta3)];
At=[-omega2*i2*cos(theta2),omega3*i3*cos(theta3);
    -omega2*i2*sin(theta2),omega3*i3*sin(theta3)];
B=[i1*sin(theta1);
    -i1*cos(theta1)];
Bt=[omega1*i1*cos(theta1);
    omega1*i1*sin(theta1)];
alpha=[A\(-At*omega+omega1*Bt)];
end

function main
%输入已知数据
clear;
i5=170;
i6=120;
e=0;
hd1 = pi/180; %一度对应的弧度
du1=180/pi;  %一弧度对应的度数
omega5=10;  %主动件角速度
alpha5=0;   %主动件角加速度

%使用子函数计算出曲柄滑块的位置,速度,加速度。
for n5=1:721
    theta1(n5)=(n5-1)*hd1;          %
    %调用函数。。。先设计出来吧]
    [theta2(n5),xc(n5),omega2(n5),vc(n5),alpha2(n5),ac(n5)]=sli_crank(theta1(n5),omega5,alpha5,i5,i6,e);
end
%图形输出开始。。。
figure(1);
n5=1:720;

%绘制位移的图
subplot(2,2,1);
[ax,h5,h6]=plotyy(theta1*du1,theta2*du1,theta1*du1,xc);
set(get(ax(1), 'ylabel'), 'String', '连杆角位移/\circ');
set(get(ax(2), 'ylabel'), 'String', '滑块位移/mm');
title('位移图');
xlabel('曲柄转角\theta_1/\circ');
grid on;
hold on;

% set(get(gca, 'PropertyName'), 'PropertyName', PropertyValue);

%速度
subplot(2,2,2);
[ax,h5,h6]=plotyy(theta1*du1,omega2,theta1*du1,vc)
title('速度图');
xlabel('曲柄转角\theta_1/\circ')
ylabel('连杆角速度/rad\cdots^{-1}')
set(get(ax(2), 'ylabel'), 'String', '滑块速度/mm\cdots^{-1}')
grid on;
hold on;

%加速度
subplot(2,2,3);
[ax,h5,h6]=plotyy(theta1*du1,alpha2,theta1*du1,ac)
title('加速度图');
xlabel('曲柄转角\theta_1/\circ')
ylabel('连杆角加速度/rad\cdots^{-2}')
set(get(ax(2), 'ylabel'), 'String', '滑块加速度/mm\cdots^{-2}')

grid on;
%绘制位置图
subplot(2,2,4)
    x(1)=0;
    y(1)=0;
    x(2)=i5*cos(70*hd1);
    y(2)=i5*sin(70*hd1);
    x(3)=xc(70);
    y(3)=e;
    x(4)=i5+i6+50;
    y(4)=0;
    x(5)=0;
    y(5)=0;
    x(6)=x(3)-40;
    y(6)=y(3)+10;
    x(7)=x(3)+40;
    y(7)=y(3)+10;
    x(8)=x(3)+40;
    y(8)=y(3)-10;
    x(9)=x(3)-40;
    y(9)=y(3)-10;
    x(10)=x(3)-40;
    y(10)=y(3)+10;
i=1:5;
plot(x(i), y(i))
grid on;
hold on;
i=6:10;
plot(x(i), y(i))
title('曲柄滑块机构');
grid on;
hold on;
xlabel('mm');
ylabel('mm');
axis([-50 400 -20 130]);
plot(x(1), y(1),'o')
plot(x(2), y(2),'o')
plot(x(3), y(3),'o')
    text(-10,-10,'A')
    text(x(2)-15,y(2)+10,'B')
    text(x(3),y(3)+20,'C')
    gtext('曲柄')
    gtext('连杆')
    gtext('滑块')
grid on;
hold on;

%运动仿真,电影制作
figure(2);
j=0;
for n5 = 1:5:360
    j=j+1;
    clf;
    x(1)=0;
    y(1)=0;
    x(2)=i5*cos(n5*hd1);
    y(2)=i5*sin(n5*hd1);
    x(3)=xc(n5);
    y(3)=e;
    x(4)=i5+i6+50;
    y(4)=0;
    x(5)=0;
    y(5)=0;
    x(6)=x(3)-40;
    y(6)=y(3)+10;
    x(7)=x(3)+40;
    y(7)=y(3)+10;
    x(8)=x(3)+40;
    y(8)=y(3)-10;
    x(9)=x(3)-40;
    y(9)=y(3)-10;
    x(10)=x(3)-40;
    y(10)=y(3)+10;
    i=1:3;
    plot(x(i), y(i));
    grid on;
    hold on;
    i=4:5;
    plot(x(i), y(i))
    i=6:10;
    plot(x(i), y(i))
    plot(x(1),y(1),'o')
    plot(x(2),y(2),'o')
    plot(x(3),y(3),'o')
    text(-10,-10,'A')
    text(x(2)-15,y(2)+10,'B')
    text(x(3),y(3)-20,'C')
    title('曲柄滑块机构');
    xlabel('mm');
    ylabel('mm');
    axis([-150 450 -150 150]);
    m(j)=getframe;
end
movie(m,2);
end

%返回连杆和滑块的参数
function[theta2,xc,omega2,vc,alpha2,ac]=sli_crank(theta1,omega1,alpha1,i1,i2,e)

    %计算连杆的角位移theta2和xc
    theta2=asin((e-i1*sin(theta1))/i2);
    xc=i1*cos(theta1)+i2*cos(theta2);

    %计算连杆和滑块的参数。。。lazy
    A=[ i2*sin(theta2),1;
        -i2*cos(theta2),0
        ];
    B=[-i1*sin(theta1);
    i1*cos(theta1)];
    omega=A\(omega1*B);
    omega2=omega(1);
    vc=omega(2);
    
    %计算连杆和滑块的加速度
    AT=[
        omega2*i2*cos(theta2),0;
        omega2*i2*sin(theta2),0
        ];
    BT=[
        -omega1*i1*cos(theta1);
        -omega1*i1*sin(theta1)
    ];
    alpha=A\(-AT*omega+alpha1*B+omega1*BT);%反应好慢。。。
    alpha2=alpha(1);
    ac=alpha(2);
end
%计算用的函数写完了,后面在调一下。

%初步写完,调试一下。

这里
修改main 成 main1 即可

img

基于new bing部分指引作答:
在给定的代码中,存在两个名为"main"的函数,这会导致错误。在MATLAB中,每个函数应该有唯一的名称。

要解决这个问题,您需要重命名其中一个"main"函数。例如,您可以将第一个"main"函数重命名为"main1",然后在代码的适当位置调用它。请注意,函数的名称应与其所在的文件名相匹配。

由于缺少一些变量定义和代码注释,可能不易理解代码逻辑。同时,在最后一行,“初步写完,调试一下。”并没有明确的问题或需要调试的部分。

因此,建议增加注释和变量定义,以及明确需要调试的部分。同时,检查代码是否有语法错误或逻辑问题。

来自GPT的回答:

根据您提供的MATLAB代码,我看到其中存在一些问题。首先,您定义了两个名为"main"的函数,这是无效的。一个MATLAB程序应该只有一个主函数。您需要将这两个函数合并为一个。

此外,您还需要在调用sli_crank函数之前定义该函数的代码。请确保您在程序中包含了sli_crank函数的定义,或者将该函数的定义复制粘贴到主函数之前。

另外,您在第一个main函数的最后有一个注释,其中包含了旧版本MATLAB中的函数moviein,但是在MATLAB 2014版本之后,该函数已被删除。您可以使用其他方法来创建动画,例如使用getframe和movie函数,或使用VideoWriter对象。

最后,请确保代码中没有其他语法错误,例如函数或变量的拼写错误或缺失的分号。

修复了这些问题后,您应该能够成功运行您的MATLAB代码。






有用望采纳,主函数不需要定义,删除第一定义即可。修改后的代码如下:


%输入已知数据
clear;
i1=75;
i2=255;
i3=158.78;
i4=257.01;
omega1=250;
alpha1=0;
hd=pi/180;
%一弧度
du=180/pi;
%2 调用子函数求出铰链的机构位移,角速度,角加速度

for n1=1:361
    theta1=(n1-1)*hd; %0 到 360°
    [theta,omega,alpha]=crank_cocker(theta1,omega1,alpha1,i1,i2,i3,i4);%返回的是一个确定的值的矩阵
    theta2(n1)=theta(1);theta3(n1)=theta(2);
    omega2(n1)=omega(1);omega3(n1)=omega(2);
    alpha2(n1)=alpha(1);alpha3(n1)=alpha(2);
end
%3 角位移,角速度,角加速度的图形输出
figure(1);%figure 是建立图形的意思。系统自动从 1,2,3,4 来建立图形,数字代表第几幅图形
n1=1:361;%建立一个行向量
subplot(2,2,1);%绘制位移线图
plot(n1,theta2*du,n1,theta3*du,'k');
title('角位移线图')%设置图形标题为。
xlabel('曲柄转角、theta_1\circ')%设置 x 轴标签
ylabel('角位移/\circ')
grid on ;%显示坐标轴网格线,grid off 则关闭坐标轴网格线
hold on;%hold on 是当前轴及图像保持而不被刷新,准备接受此后将绘制的图形,多图共存。hold off(默认)则相反
text(140,170,'\theta_3') %表示在坐标 (140,170) 处添加文本'\theta_3'
text(140,30 ,'\theta_2')
%绘制角速度线图
subplot(2,2,2);
plot(n1,omega2,n1,omega3,'k')
title('角速度线图')
xlabel('曲柄转角、theta_1\circ')
ylabel('角速度/rad\cdots^{-1}')
grid on;hold on;
text(250,130,'\omega_2')
text(130,165,'\omega_3')
%角加速度线图
subplot(2,2,3);
plot(n1,alpha2,n1,alpha3,'k')
title('角加速度线图');
xlabel('曲柄转角、theta_1/\circ')
xlabel('角加速度/rad\cdots^{-2}')
grid on;hold on;
text(230,2e4,'\alpha_2')
text(30,7e4,'\alpha_3')
%铰链四杆机构图形输出
subplot(2,2,4);
x(1)=0;y(1)=0;
x(2)=i1*cos(70*hd);y(2)=i1*sin(70*hd);
x(3)=i4+i3*cos(theta3(71));y(3)=i3*sin(theta3(71));
x(4)=i4;y(4)=0;
x(5)=0;y(5)=0;
grid on;hold on;
plot(x,y)
plot(x(1),y(1),'o')
plot(x(2),y(2),'o')
plot(x(3),y(3),'o')
plot(x(4),y(4),'o')%描点,点的格式是小圆
title('铰链四杆机构')
xlabel('mm');
axis([-50 350 -20 200]);
%gtext(‘sinx’)
%4 铰链四杆机构运动仿真
figure(2)
%创建电影动画的开始
m=moviein(20)%这个函数在 2014 版本之后已经无效了
j=0;
for n1=1:5:360
    j=j+1;
    clf;
    x(1)=0;
    y(1)=0;
    x(2)=i1*cos((n1-1)*hd);
    y(2)=i1*sin((n1-1)*hd);
    x(3)=i4+i3*cos(theta3(n1));
    y(3)=i3*sin(theta3(n1));
    x(4)=i4;
    y(4)=0;
    x(5)=0;
    y(5)=0;
    plot(x,y);
    grid on;
    hold on;
    plot(x(1),y(1),'o');
    plot(x(2),y(2),'o');
    plot(x(3),y(3),'o');
    plot(x(4),y(4),'o');
    axis([-200 350 -150 200]);%axis([xmin xmax ymin   ymax]) [ ] 中分别给出 x 轴和 y 轴的最大值、最小值
    title('铰链四杆机构');
    xlabel('mm');ylabel('mm');
    %以上都是用来生成图形的,画 n1=X 时的图形
    m(j)=getframe;
    %{该函数格式有:
    % (1)F=gefframe,从当前图形框中得到动画帧
    % (2)F=gefframe(h),从图形句柄 h 中得到动画帧
    % (3)F=getframe(h,rect),从图形句柄 h 的指定区域 rec 中得到动画帧
end
movie(m,2);
% 该函数的主要格式有:
% (1)movie(M),将矩阵 M 中的动画帧播放一次
% (2)movie(M,n),将矩阵 M 中的动画帧播放 n 次
% (3)movie(M,n,fps),将矩阵 M 中的动画帧以每秒 fps 帧的速度播放 n 次
%movie2avi()
%创建动画电影的步骤:
%001——》调用 moviein 函数对内存进行初始化(该步骤在 Matlab5.3 以上均可省略),
%         创建一个足够大的矩阵,使之能够容纳基于当前坐标轴大小的一系列指定的图形(此处称为帧)。
%002——》%使用 getframe 调用 getframe 函数生成每个帧。该函数返回一个列矢量,利用这个矢量,
%就可以创建一个电影动画矩阵.
%003——》调用 movie 函数按照指定的速度和次数运行该电影动画。
%004——》调用 movie2avi 函数可以将矩阵中的一系列动画帧转换成视频文件 avi 文件。
%         这样,即使脱离了 matlab 环境都可以播放动画。

function[theta,omega,alpha]=crank_cocker(theta1,omega1,~,i1,i2,i3,i4)
%1. 计算从动件角位移
L=sqrt(i4*i4+i1*i1-2*i1*i4*cos(theta1));
phi=asin((i1./L)*sin(theta1));
beta=acos((L*L+i3*i3-i2*i2)/(2*i3*L));
if beta<0
    beta=beta+pi;
end
theta3=pi-phi-beta;%theta3 是杆 3 转过的角度
theta2=asin((i3*sin(theta3)-i1*sin(theta1))/i2);%theta2 是杆 2 转过的角度
theta=[theta2;theta3];

%2. 计算从动件的角速度
A=[-i2*sin(theta2),i3*sin(theta3);
    i2*cos(theta2),-i3*cos(theta3)];%机构从动件位置参数矩阵
B=[i1*sin(theta1);-i1*cos(theta1)];  %原动件位置参数矩阵
omega=A\(omega1*B);
omega2=omega(1);omega3=omega(2);
%3. 计算从动件的角加速度
A=[-i2*sin(theta2),i3*sin(theta3);
    i2*cos(theta2),-i3*cos(theta3)];
At=[-omega2*i2*cos(theta2),omega3*i3*cos(theta3);
    -omega2*i2*sin(theta2),omega3*i3*sin(theta3)];
B=[i1*sin(theta1);
    -i1*cos(theta1)];
Bt=[omega1*i1*cos(theta1);
    omega1*i1*sin(theta1)];
alpha=[A\(-At*omega+omega1*Bt)];
end

function main
%输入已知数据
clear;
i5=170;
i6=120;
e=0;
hd1 = pi/180; %一度对应的弧度
du1=180/pi;  %一弧度对应的度数
omega5=10;  %主动件角速度
alpha5=0;   %主动件角加速度

%使用子函数计算出曲柄滑块的位置,速度,加速度。
for n5=1:721
    theta1(n5)=(n5-1)*hd1;          %
    %调用函数。。。先设计出来吧]
    [theta2(n5),xc(n5),omega2(n5),vc(n5),alpha2(n5),ac(n5)]=sli_crank(theta1(n5),omega5,alpha5,i5,i6,e);
end
%图形输出开始。。。
figure(1);
n5=1:720;

%绘制位移的图
subplot(2,2,1);
[ax,h5,h6]=plotyy(theta1*du1,theta2*du1,theta1*du1,xc);
set(get(ax(1), 'ylabel'), 'String', '连杆角位移/\circ');
set(get(ax(2), 'ylabel'), 'String', '滑块位移/mm');
title('位移图');
xlabel('曲柄转角\theta_1/\circ');
grid on;
hold on;

% set(get(gca, 'PropertyName'), 'PropertyName', PropertyValue);

%速度
subplot(2,2,2);
[ax,h5,h6]=plotyy(theta1*du1,omega2,theta1*du1,vc)
title('速度图');
xlabel('曲柄转角\theta_1/\circ')
ylabel('连杆角速度/rad\cdots^{-1}')
set(get(ax(2), 'ylabel'), 'String', '滑块速度/mm\cdots^{-1}')
grid on;
hold on;

%加速度
subplot(2,2,3);
[ax,h5,h6]=plotyy(theta1*du1,alpha2,theta1*du1,ac)
title('加速度图');
xlabel('曲柄转角\theta_1/\circ')
ylabel('连杆角加速度/rad\cdots^{-2}')
set(get(ax(2), 'ylabel'), 'String', '滑块加速度/mm\cdots^{-2}')

grid on;
%绘制位置图
subplot(2,2,4)
x(1)=0;
y(1)=0;
x(2)=i5*cos(70*hd1);
y(2)=i5*sin(70*hd1);
x(3)=xc(70);
y(3)=e;
x(4)=i5+i6+50;
y(4)=0;
x(5)=0;
y(5)=0;
x(6)=x(3)-40;
y(6)=y(3)+10;
x(7)=x(3)+40;
y(7)=y(3)+10;
x(8)=x(3)+40;
y(8)=y(3)-10;
x(9)=x(3)-40;
y(9)=y(3)-10;
x(10)=x(3)-40;
y(10)=y(3)+10;
i=1:5;
plot(x(i), y(i))
grid on;
hold on;
i=6:10;
plot(x(i), y(i))
title('曲柄滑块机构');
grid on;
hold on;
xlabel('mm');
ylabel('mm');
axis([-50 400 -20 130]);
plot(x(1), y(1),'o')
plot(x(2), y(2),'o')
plot(x(3), y(3),'o')
text(-10,-10,'A')
text(x(2)-15,y(2)+10,'B')
text(x(3),y(3)+20,'C')
gtext('曲柄')
gtext('连杆')
gtext('滑块')
grid on;
hold on;

%运动仿真,电影制作
figure(2);
j=0;
for n5 = 1:5:360
    j=j+1;
    clf;
    x(1)=0;
    y(1)=0;
    x(2)=i5*cos(n5*hd1);
    y(2)=i5*sin(n5*hd1);
    x(3)=xc(n5);
    y(3)=e;
    x(4)=i5+i6+50;
    y(4)=0;
    x(5)=0;
    y(5)=0;
    x(6)=x(3)-40;
    y(6)=y(3)+10;
    x(7)=x(3)+40;
    y(7)=y(3)+10;
    x(8)=x(3)+40;
    y(8)=y(3)-10;
    x(9)=x(3)-40;
    y(9)=y(3)-10;
    x(10)=x(3)-40;
    y(10)=y(3)+10;
    i=1:3;
    plot(x(i), y(i));
    grid on;
    hold on;
    i=4:5;
    plot(x(i), y(i))
    i=6:10;
    plot(x(i), y(i))
    plot(x(1),y(1),'o')
    plot(x(2),y(2),'o')
    plot(x(3),y(3),'o')
    text(-10,-10,'A')
    text(x(2)-15,y(2)+10,'B')
    text(x(3),y(3)-20,'C')
    title('曲柄滑块机构');
    xlabel('mm');
    ylabel('mm');
    axis([-150 450 -150 150]);
    m(j)=getframe;
end
movie(m,2);
end

%返回连杆和滑块的参数
function[theta2,xc,omega2,vc,alpha2,ac]=sli_crank(theta1,omega1,alpha1,i1,i2,e)

%计算连杆的角位移theta2和xc
theta2=asin((e-i1*sin(theta1))/i2);
xc=i1*cos(theta1)+i2*cos(theta2);

%计算连杆和滑块的参数。。。lazy
A=[ i2*sin(theta2),1;
    -i2*cos(theta2),0
    ];
B=[-i1*sin(theta1);
    i1*cos(theta1)];
omega=A\(omega1*B);
omega2=omega(1);
vc=omega(2);

%计算连杆和滑块的加速度
AT=[
    omega2*i2*cos(theta2),0;
    omega2*i2*sin(theta2),0
    ];
BT=[
    -omega1*i1*cos(theta1);
    -omega1*i1*sin(theta1)
    ];
alpha=A\(-AT*omega+alpha1*B+omega1*BT);%反应好慢。。。
alpha2=alpha(1);
ac=alpha(2);
end
%计算用的函数写完了,后面在调一下。

%初步写完,调试一下。

img

img

应也是如此。下面是一些常用的matlab调试运行方法:

  1. 在程序中添加断点。断点可以让程序在执行到某一行代码时停下来,以便查看变量的值,走到下一行之前可以进行修改或调试。在编辑器中点击行号即可在该行添加/移除断点,并在调试模式下运行程序即可使用。

  2. 针对某一段代码进行单独运行。有时候程序出错并不在整个程序的情况下,可以使用"Run to selected function"或"Run section"的功能在调试模式下仅运行选中的代码段。

  3. 使用debugger。MathWorks官方debugger是一个用来调试matlab程序的图形化界面,可以在程序执行时暂停程序,查看变量的值,执行某一行代码或某个函数调用等。

  4. 使用try-catch语句。在程序中包含try-catch语句可以捕捉程序中抛出的异常,并在出错时执行某些特定的操作。

  5. 使用assert。assert可以在代码中添加判断条件,如果条件不符合,则程序停止执行并显示错误信息。

  6. 使用Matlab工具箱。MathWorks提供了一系列的工具箱,其中包括debugger、profiler等,可以帮助用户更好的调试程序和优化程序性能。

以上是一些常见的matlab调试运行方法,可以根据实际情况选择适合自己的方法进行调试。

一个函数名(main)不能实例化多次,换个名称

错误提示你,main函数已在此作用域中申明,意思就是main这个函数已经申明过了,就不能再重复申明,检查下你代码中,应该是有多个函数的名称叫main,你只需要将重名的函数名称换一个名称即可,比如main2、main3、main4等随便叫一个名称都可以的,只要不重复。

语法好多报错呢,还需要解决吗

这提示很明显 主函数重复了

img


150行和第一行的main名称重复了