Matlab基础学习(下)

Contents

随机数:rank函数

%调用格式:rand(n,m)生成n行m列处于0~1之间的随机数
a=rand(3,4)            %3行4列0到1的随机数矩阵
%随机整数:rand([min,max],n,m),生成[min,max]范围内n行m列的随机整数矩阵
randi([-2,10],2,3)     %生成2行3列-2到10的随机矩阵
%生成正态分布
y=randn(1,9000)+3      %+3就是生成以3为中心的正态分布随机数
histogram(y,100)       %画直方图
%生成任何范围的随机数,在rand前面乘上一个数即获得该数乘以0~1范围中的随机数
a=2*pi*rand(2,3)       %生成2行3列,数值在[0 2pi]之间的随机数
b=2+(2*pi-2)*rand(1,6) %左边界不为0,[2 2pi]的随机数

生成任何范围的随机数获得机器人运动空间1

%机器人工作空间-蒙特卡洛法
q1min=-pi/4;
q1max=pi/4;
q2min=-2*pi/3;
q2max=pi/18;
q3min=-pi/2;
q3max=pi/2;
q4min=-4*pi/3;
q4max=pi/3;
q5min=-pi/4;
q5max=pi/4;%关节范围
a1=20;a2=320;a3=310;%DH参数
n=20000;
q1=q1min+(q1max-q1min).*rand(n,1);
q2=q2min+(q2max-q2min).*rand(n,1);
q3=q1min+(q3max-q3min).*rand(n,1);
q4=q1min+(q4max-q4min).*rand(n,1);
q5=q1min+(q5max-q5min).*rand(n,1);
px=a1.*cos(q1) + a3.*(cos(q1).*cos(q2).*cos(q3) - cos(q1).*sin(q2).*sin(q3)) + a2.*cos(q1).*cos(q2)
py=a1.*sin(q1) + a3.*(cos(q2).*cos(q3).*sin(q1) - sin(q1).*sin(q2).*sin(q3)) + a2.*cos(q2).*sin(q1)
pz=- a2.*sin(q2) - a3.*(cos(q2).*sin(q3) + cos(q3).*sin(q2)) 
subplot(2,2,1)
plot3(px,py,pz,'*')
title('机械臂三维工作空间')
xlabel('X/mm')
ylabel('Y/mm')
zlabel('Z/mm')
grid on
subplot(2,2,2)
plot3(px,py,pz,'*')
title('机械臂XOY平面工作空间')
xlabel('X/mm')
ylabel('Y/mm')
zlabel('Z/mm')
grid on
view(0,90)%XOY平面
subplot(2,2,3)
plot3(px,py,pz,'*')
title('机械臂XOZ平面工作空间')
xlabel('X/mm')
ylabel('Y/mm')
zlabel('Z/mm')
grid on
view(0,0)%XOZ平面  
subplot(2,2,4)
plot3(px,py,pz,'*')
 title('机械臂YOZ平面工作空间')
xlabel('X/mm')
ylabel('Y/mm')
zlabel('Z/mm')
grid on
view(90,0)%YOZ平面

Baxter机器人左臂工作空间

Left Arm          Theta   d              a             alpha r/p theta offset

Ll(1) = Link ([0    0.27035  0.069  -pi/2  0    0], 'standard'); % start at joint s0 and move to joint s1
Ll(2) = Link ([0    0        0   pi/2  0    pi/2], 'standard');        % start at joint s1 and move to joint e0
Ll(3) = Link ([0    0.36435  0.0690      -pi/2  0    0], 'standard'); % start at joint e0 and move to joint e1
Ll(4) = Link ([0    0        0    pi/2  0    0], 'standard');           % start at joint e1 and move to joint w0
Ll(5) = Link ([0    0.37429  0.010    -pi/2  0    0], 'standard');  % start at joint w0 and move to joint w1
Ll(6) = Link ([0    0        0    pi/2  0    0], 'standard');           % start at joint w1 and move to joint w2
Ll(7) = Link ([0    0.229525 0    0     0    0], 'standard');         % start at joint w2 and move to end-effector

Baxter_Left = SerialLink(Ll, 'name', 'Baxter Left Arm', 'base' , ...
                      transl(0.024645, 0.219645, 0.118588) * trotz(pi/4)...
                      * transl(0.055695, 0, 0.011038));
% 蒙特卡洛随机算法
clf
hold on;N=3000; %定义随机采样数,一般越多越精确,不过耗时。
for n=1:1:3000                               %
theta1=-141/180*pi+(141/180*pi+51/180*pi)*rand(N,1); %limit of joint1
theta2=-123/180*pi+(123/180*pi+60/180*pi)*rand(N,1);   %limit of joint2
theta3=-173/180*pi+(173/180*pi+173/180*pi)*rand(N,1);  %limit of joint3
theta4=-3/180*pi+(3/180*pi+150/180*pi)*rand(N,1); %limit of joint4
theta5=-175/180*pi+(175/180*pi+175/180*pi)*rand(N,1); %limit of joint5
theta6=-90/180*pi+(90/180*pi+120/180*pi)*rand(N,1); %limit of joint6
theta7=-175/180*pi+(175/180*pi+175/180*pi)*rand(N,1);%limit of joint7
qq=[theta1(n),theta2(n),theta3(n),theta4(n),theta5(n),theta6(n),theta7(n)];%随机采样的7关节向量
Mricx=Baxter_Left.fkine(qq);%正解函数得到末端位姿矩阵
hold on;
view(3)
Baxter_Left.plot(qq)
plot3(Mricx.t(1),Mricx.t(2),Mricx.t(3),'b*','MarkerSize',2);%从末端位姿矩阵中提取位置矢量并plot3画出
pause(0.001)
end 

线性规划:linprog函数,运筹学的重要分支之一。需要优化工具箱(optimization toolbox)

%(运筹学(operational research)是一门解决一定约束条件下最优解的学科。
left=[-1 1;-1 -1;2 1;];%不等式左边,一个分号表示一组不等式方程
right=[1 -1 2]; %不等式右边,构成的不等式组约束为:-x+y<=1;-x-y<=-1;2x+y<=2
f=[1 2];          %f=x+2y,目标函数
[x min]=linprog(f,left,right)%目标函数在上面约束条件下的最小值
%x为返回的自变量取何值时最小,min为返回的最小为多少数(四舍五入)x=[1.2 2.9 -3.2 -3.7 0]
round(x)
%ound(x,n)其中n>0对小数部分四舍五入到第n位
ans1=round(pi , 3)
%round(x,n) 其中n<0对整数部分四舍五入到第n位
ans2=round(12355.123 , -2)%对12355.123的十位进行四舍五入

gcd函数用于求最大公约数,lcm函数用于求最小公倍数

x=81,y=9
ans1=gcd(x,y)       %最大公约数
ans2=sym( lcm(x,y) )%最小公倍数 
isprime函数用于求质数操作
x=1:10       %从1到10
pos=isprime(x)  %pos保存0 1矩阵,为1的对应质数
ans=x(pos==1)   %输出质数从1到10,是质数的数字

find函数:查询矩阵中满足条件元素的位置

x= [1 0 2; 0 1 1; 0 0 4]
ans1=find(x,3)         %先列后行的输出答案---不是非0的数,而是所在位置
ans2=find(x==2,1)     %找出X中为2的数所在位置,find函数第一个作为条件
%这样通过find函数找到数的位置后,便可以将其输出
x(7)  %输出x中的第7个数,先列后行
ans3=find(x>=2 & x<=4,2)%多条件查询

快速生成表格,table结构体的组合,每一行时一个元胞数组

name = {'Mike';'John';'Sam';'Bob';'Brown'}; %字符串要用大括号
age = [38;43;38;40;49];
height = [71;69;64;67;64];
weight = [176;163;131;133;119];
bloodpressure = [124 93; 109 77; 125 83; 117 75; 122 80];
node=table(name,age,height,weight,bloodpressure)
%查询第一个结构体的第二个数据(age)
node(1,2)
%添加一列BMI
node.BMI=(node.weight*0.4)./(node.height*0.03).^2 %点运算

三维插值

clf
x=1+4*rand(5,1);
y=4*rand(1,5); %这里都用的是随机数生成三维离散点
z=2+rand(5,5)*3; %注意z要求生成的是二维矩阵,行列数分别于x,y的大小对应
subplot(1,3,1)  %figure分区,一行俩列,第一列
plot3(x,y,z,'r.')
title('插值前')
hold on
grid on
%
subplot(1,3,2)
plot3(x,y,z,'r.')
hold on
grid on
title('插值后')
subplot(1,3,3)
[x1,y1]=meshgrid(1:0.01:5,0:0.01:4); %生成新的精度坐标
z1=griddata(x,y,z,x1,y1,'v4'); %插值
surf(x1,y1,z1,'FaceAlpha',0.1);
hold on
title('三维曲面')

读取excel文件,用WPS重新保存为微软格式的xls文件表格

%[data1,data2,data3]=xlsread('你要读取的文件名',sheet,range),
%data1:返回数值数据
%data2:返回文本数据
%data3:返回所有数据
Data=xlsread('D:\Matlab\work\ABB.xls');%返回所有数据,文本的不在
Time=Data(:,1);    %读取第1列
Power=Data(:,2);    %读取第2列
Energy=Data(:,3);    %读取第3列
clf
plot(Power,'c-','LineWidth',0.5)%试着画出来功率图
legend( 'Power')
grid on
ylabel('瓦特(W)'),xlabel('时间/S')
Published with MATLAB® R2018a

以上.

以下为所有代码复制到脚本,按小节运行:

%% 随机数:rank函数
%调用格式:rand(n,m)生成n行m列处于0~1之间的随机数
a=rand(3,4)            %3行4列0到1的随机数矩阵
%随机整数:rand([min,max],n,m),生成[min,max]范围内n行m列的随机整数矩阵
randi([-2,10],2,3)     %生成2行3列-2到10的随机矩阵
%生成正态分布
y=randn(1,9000)+3      %+3就是生成以3为中心的正态分布随机数
histogram(y,100)       %画直方图
%生成任何范围的随机数,在rand前面乘上一个数即获得该数乘以0~1范围中的随机数
a=2*pi*rand(2,3)       %生成2行3列,数值在[0 2pi]之间的随机数
b=2+(2*pi-2)*rand(1,6) %左边界不为0,[2 2pi]的随机数
%% 生成任何范围的随机数获得机器人运动空间1
%机器人工作空间-蒙特卡洛法
q1min=-pi/4;
q1max=pi/4;
q2min=-2*pi/3;
q2max=pi/18;
q3min=-pi/2;
q3max=pi/2;
q4min=-4*pi/3;
q4max=pi/3;
q5min=-pi/4;
q5max=pi/4;%关节范围
a1=20;a2=320;a3=310;%DH参数
n=20000;
q1=q1min+(q1max-q1min).*rand(n,1);
q2=q2min+(q2max-q2min).*rand(n,1);
q3=q1min+(q3max-q3min).*rand(n,1);
q4=q1min+(q4max-q4min).*rand(n,1);
q5=q1min+(q5max-q5min).*rand(n,1);
px=a1.*cos(q1) + a3.*(cos(q1).*cos(q2).*cos(q3) - cos(q1).*sin(q2).*sin(q3)) + a2.*cos(q1).*cos(q2)
py=a1.*sin(q1) + a3.*(cos(q2).*cos(q3).*sin(q1) - sin(q1).*sin(q2).*sin(q3)) + a2.*cos(q2).*sin(q1)
pz=- a2.*sin(q2) - a3.*(cos(q2).*sin(q3) + cos(q3).*sin(q2)) 
subplot(2,2,1)
plot3(px,py,pz,'*')
title('机械臂三维工作空间')
xlabel('X/mm')
ylabel('Y/mm')
zlabel('Z/mm')
grid on
subplot(2,2,2)
plot3(px,py,pz,'*')
title('机械臂XOY平面工作空间')
xlabel('X/mm')
ylabel('Y/mm')
zlabel('Z/mm')
grid on
view(0,90)%XOY平面
subplot(2,2,3)
plot3(px,py,pz,'*')
title('机械臂XOZ平面工作空间')
xlabel('X/mm')
ylabel('Y/mm')
zlabel('Z/mm')
grid on
view(0,0)%XOZ平面  
subplot(2,2,4)
plot3(px,py,pz,'*')
 title('机械臂YOZ平面工作空间')
xlabel('X/mm')
ylabel('Y/mm')
zlabel('Z/mm')
grid on
view(90,0)%YOZ平面
%% Baxter机器人左臂工作空间
% Left Arm
%             Theta d     a                                  alpha r/p  theta offset
Ll(1) = Link ([0    0.27035  0.069                             -pi/2  0    0], 'standard'); % start at joint s0 and move to joint s1
Ll(2) = Link ([0    0        0                                  pi/2  0    pi/2], 'standard');        % start at joint s1 and move to joint e0
Ll(3) = Link ([0    0.36435  0.0690                            -pi/2  0    0], 'standard'); % start at joint e0 and move to joint e1
Ll(4) = Link ([0    0        0                                  pi/2  0    0], 'standard');           % start at joint e1 and move to joint w0
Ll(5) = Link ([0    0.37429  0.010                             -pi/2  0    0], 'standard');  % start at joint w0 and move to joint w1
Ll(6) = Link ([0    0        0                                  pi/2  0    0], 'standard');           % start at joint w1 and move to joint w2
Ll(7) = Link ([0    0.229525 0                                  0     0    0], 'standard');         % start at joint w2 and move to end-effector

Baxter_Left = SerialLink(Ll, 'name', 'Baxter Left Arm', 'base' , ...
                      transl(0.024645, 0.219645, 0.118588) * trotz(pi/4)...
                      * transl(0.055695, 0, 0.011038));
% 蒙特卡洛随机算法
clf
hold on;N=3000; %定义随机采样数,一般越多越精确,不过耗时。
for n=1:1:3000                               %
theta1=-141/180*pi+(141/180*pi+51/180*pi)*rand(N,1); %limit of joint1
theta2=-123/180*pi+(123/180*pi+60/180*pi)*rand(N,1);   %limit of joint2
theta3=-173/180*pi+(173/180*pi+173/180*pi)*rand(N,1);  %limit of joint3
theta4=-3/180*pi+(3/180*pi+150/180*pi)*rand(N,1); %limit of joint4
theta5=-175/180*pi+(175/180*pi+175/180*pi)*rand(N,1); %limit of joint5
theta6=-90/180*pi+(90/180*pi+120/180*pi)*rand(N,1); %limit of joint6
theta7=-175/180*pi+(175/180*pi+175/180*pi)*rand(N,1);%limit of joint7
qq=[theta1(n),theta2(n),theta3(n),theta4(n),theta5(n),theta6(n),theta7(n)];%随机采样的7关节向量
Mricx=Baxter_Left.fkine(qq);%正解函数得到末端位姿矩阵
hold on;
view(3)
Baxter_Left.plot(qq)
plot3(Mricx.t(1),Mricx.t(2),Mricx.t(3),'b*','MarkerSize',2);%从末端位姿矩阵中提取位置矢量并plot3画出
pause(0.001)
end
%% 线性规划:linprog函数,运筹学的重要分支之一。需要优化工具箱(optimization toolbox)
%(运筹学(operational research)是一门解决一定约束条件下最优解的学科。
left=[-1 1;-1 -1;2 1;];%不等式左边,一个分号表示一组不等式方程
right=[1 -1 2]; %不等式右边,构成的不等式组约束为:-x+y<=1;-x-y<=-1;2x+y<=2
f=[1 2];          %f=x+2y,目标函数
[x min]=linprog(f,left,right)%目标函数在上面约束条件下的最小值
%x为返回的自变量取何值时最小,min为返回的最小为多少
%% round函数(四舍五入)
x=[1.2 2.9 -3.2 -3.7 0]
round(x)
%ound(x,n)其中n>0对小数部分四舍五入到第n位
ans1=round(pi , 3)
%round(x,n) 其中n<0对整数部分四舍五入到第n位
ans2=round(12355.123 , -2)%对12355.123的十位进行四舍五入
%% gcd函数用于求最大公约数,lcm函数用于求最小公倍数
x=81,y=9
ans1=gcd(x,y)       %最大公约数
ans2=sym( lcm(x,y) )%最小公倍数
%% isprime函数用于求质数操作
x=1:10       %从1到10
pos=isprime(x)  %pos保存0 1矩阵,为1的对应质数
ans=x(pos==1)   %输出质数从1到10,是质数的数字
%% find函数:查询矩阵中满足条件元素的位置
x= [1 0 2; 0 1 1; 0 0 4]
ans1=find(x,3)         %先列后行的输出答案---不是非0的数,而是所在位置
ans2=find(x==2,1)     %找出X中为2的数所在位置,find函数第一个作为条件
%这样通过find函数找到数的位置后,便可以将其输出
x(7)  %输出x中的第7个数,先列后行
ans3=find(x>=2 & x<=4,2)%多条件查询
%% 快速生成表格,table结构体的组合,每一行时一个元胞数组
name = {'Mike';'John';'Sam';'Bob';'Brown'}; %字符串要用大括号
age = [38;43;38;40;49];
height = [71;69;64;67;64];
weight = [176;163;131;133;119];
bloodpressure = [124 93; 109 77; 125 83; 117 75; 122 80];
node=table(name,age,height,weight,bloodpressure)
%查询第一个结构体的第二个数据(age)
node(1,2) 
%添加一列BMI
node.BMI=(node.weight*0.4)./(node.height*0.03).^2 %点运算
%% 三维插值
clf
x=1+4*rand(5,1);
y=4*rand(1,5); %这里都用的是随机数生成三维离散点
z=2+rand(5,5)*3; %注意z要求生成的是二维矩阵,行列数分别于x,y的大小对应
subplot(1,3,1)  %figure分区,一行俩列,第一列
plot3(x,y,z,'r.')
title('插值前')
hold on
grid on
%
subplot(1,3,2)
plot3(x,y,z,'r.')
hold on
grid on
title('插值后')
subplot(1,3,3)
[x1,y1]=meshgrid(1:0.01:5,0:0.01:4); %生成新的精度坐标
z1=griddata(x,y,z,x1,y1,'v4'); %插值
surf(x1,y1,z1,'FaceAlpha',0.1);
hold on
title('三维曲面')
%% 读取excel文件,用WPS重新保存为微软格式的xls文件表格
%[data1,data2,data3]=xlsread('你要读取的文件名',sheet,range),
%data1:返回数值数据
%data2:返回文本数据
%data3:返回所有数据
Data=xlsread('D:\Matlab\work\ABB.xls');%返回所有数据,文本的不在
Time=Data(:,1);    %读取第1列
Power=Data(:,2);    %读取第2列
Energy=Data(:,3);    %读取第3列
clf
plot(Power,'c-','LineWidth',0.5)%试着画出来功率图
legend( 'Power')
grid on
ylabel('瓦特(W)'),xlabel('时间/S')

Last Words:matlab基础学习官网链接传送门

https://ww2.mathworks.cn/help/matlab/examples.html?category=index&s_tid=CRUX_VM_example_index

猜你喜欢

转载自blog.csdn.net/weixin_39090239/article/details/105905502