MATLAB笔记
B1 对数、赋值、向量
>> %matlab矩阵实验室,数学,科学计算
>> %command window:命令行窗口
>> %命令基本相同
>> %matlab使用计算工具,加减乘除
>> 1+1
ans =
2
>> 2+3
ans =
5
>> 2-3
ans =
-1
>> %Workspace:
>> %Command History历史记录:可清楚
>> %Command History历史记录:可清除
>> %乘方,求对数。。。
>> 5^2
ans =
25
>> 2^5
ans =
32
>> %对数
>> %log默认代表ln
>> log(32)
ans =
3.4657
>> %自然对数e为底数
>> log2(32)
ans =
5
>> %2为底数,log2,log10,log为特殊情况,其余值不能运算
>> %log8(7)=log(7)/log(8)
>> log(7)/log(8)
ans =
0.9358
>> %计算log2,log10,log以为的情况
>> %clc为清空屏幕
>> x=10
x =
10
>> y=5
y =
5
>> x+y
ans =
15
>> %x,y赋值被保存到工作区内
>> %方程,eg:x+1=2
>> %计算机无法求解上述问题,只能赋值,不能求解,需要人为设计算法
>> %“=”叫做赋值符号,即assign
>> %x,y是一个存放数字的空间
>> x=1
x =
1
>> x=2
x =
2
>> x=3
x =
3
>> %工作区变量跟随命令行窗口中的赋值同步变化
>> %赋值案例
>> %x=3
>> %x=x+1
>> %则x=4
>>
>> %【向量】
>> x=[1 2 3]
x =
1 2 3
>> x*2
ans =
2 4 6
>> x
x =
1 2 3
>> %x*2不等于赋值给
>> x=x*2
x =
2 4 6
>> %【向量相加】
>> x=[1 2 3];
>> y=[5 6 7];
>> x+y
ans =
6 8 10
>> %只有x和y维度相同的时候才可以相加
B2 矩阵、脚本
>> %x不仅可以表示数字,还可以表示向量和矩阵
>> x=[1 2 3]
x =
1 2 3
>> x=[1 2 3;4 5 6;7 9 8]
x =
1 2 3
4 5 6
7 9 8
>> %跟向量一样,矩阵也可以做加法运算
>> A=[1 2;1 2]
A =
1 2
1 2
>> B=[3 4;5 6]
B =
3 4
5 6
>> A+B
ans =
4 6
6 8
>> A-B
ans =
-2 -2
-4 -4
>> %矩阵乘法
>> A*B
ans =
13 16
13 16
>> %矩阵乘法:
>> %a11=1*3+2*5=13 a12=1*4+2*6=16
>> %a21=1*3+2*5=13 a22=1*4+2*6=16
>> A.*B
ans =
3 8
5 12
>> %A*B:矩阵运算,A.*B相同位置元素相乘
>> %【脚本】
>> %New Script
>> %x=5 y=7 x+y
>> %脚本:把一堆命令组合在一起
>> %保存脚本:D:\ing\matlab\NewScript
>> NewScripts
x =
5
y =
7
ans =
12
>> %New Script2
>> %x=5; y=7; x+y
ans =
12
>> %以上为脚本运行结果:打开-运行
>> %无分号时,代码中的=下方有红色波纹(影响不大)x和y的值都会保存在内存中
>> %脚本增加程序的互动性
>> NewScripts
please enter x15
>> NewScripts
please enter x6
please enter y10
ans =
16
>> %脚本内容
x=input('please enter x');
y=input('please enter y');
x+y
>> tutorial2_3
Enter a temperature in Celsius:20
F =
68
>> %摄氏温度20度转换为华氏温度是68度
>> %脚本内容
C=input('Enter a temperature in Celsius:');
F=(C*1.8)+32;
F
B3
tutorial2-3
C=input('Enter a temperature in Celsius:');
F=(C*1.8)+32;
F
>> tutorial2_3
Enter a temperature in Celsius:45
F =
113
tutorial3-1
C=input('Enter a temperature in Celsius:');
F=(C*1.8)+32;
fprintf('Fahrenheit= %f', F);
>> tutorial3_1
Enter a temperature in Celsius:45
Fahrenheit= 113.000000>>
换行
C=input('Enter a temperature in Celsius:');
F=(C*1.8)+32;
fprintf('Fahrenheit= %f\n', F); %"\n"换行
>> tutorial3_1
Enter a temperature in Celsius:45
Fahrenheit= 113.000000
>>
tutorial2_2
x=input('please enter x:');
y=input('please enter y:');
x+y
>> tutorial2_2
please enter x:5
please enter y:6
ans =
11
简化
x=input('please enter x:');
y=input('please enter y:');
fprintf('%f+%f=%f\n',x,y,x+y);
>> tutorial2_2
please enter x:6
please enter y:7
6.000000+7.000000=13.000000
>>
除去多余的零:把 f 换成 g
x=input('please enter x:');
y=input('please enter y:');
fprintf('%g+%g=%g\n',x,y,x+y);
please enter x:6
please enter y:7
6+7=13
>>
转换公式
*Fahrenheit to Celsius: (°F-32)÷1.8=°C
*Celsius to Fahrenheit: (°C×1.8)÷32=°F
######if命令
if 条件
(else)
end
num=input('Please enter a number'); %请输入一个数字num,判断num是正数还是负数
if num > 0
fprintf('Positive\n'); %如果num大于0,那么num是一个正数
else
fprintf('Negative\n'); %否则是一个负数
end
tutorial3-2
>> tutorial3_2
Please enter a number-1
Negative
>> tutorial3_2
Please enter a number7
Positive
>> tutorial3_2
Please enter a number0
Negative
>> %bug为0情况
bug修复,优化代码
num=input('Please enter a number');
if num > 0
fprintf('Positive\n');
elseif num <0
fprintf('Negative\n');
else
fprintf('num=0\n')
end
>> tutorial3_2
Please enter a number10
Positive
>> tutorial3_2
Please enter a number-2
Negative
>> tutorial3_2
Please enter a number0
num=0
>>
比较
a=input('a=');
b=input('b=');
if a > b
fprintf('%g\n',a); %若a>b,输出a
else
fprintf('%g\n',b); %否则输出b
end
tutorial3-3
>> tutorial3_3
a=-2
b=-9
g
>> tutorial3_3
a=-2
b=-9
-2
>> tutorial3_3
a=1
b=10
10
>> tutorial3_3
a=8
b=8
8
优化,美化
a=input('a=');
b=input('b=');
if a > b
fprintf('%g is greater.\n',a);
else
fprintf('%g is greater.\n',b);
end
>> tutorial3_3
a=6
b=3
6 is greater.
判断三角形
tutorial3-4
三个条件逐次判断
a=input('Enter the value of a:');
b=input('Enter the value of b:');
c=input('Enter the value of c:');
if a+b<=c
fprintf('No\n');
elseif a+c<=b
fprintf('No\n');
elseif b+c<=a
fprintf('No\n');
else
fprintf('Yes\n');
end
tutorial3-4
>> tutorial3_4
Enter the value of a:7
Enter the value of b:6
Enter the value of c:5
Yes
>> tutorial3_4
Enter the value of a:3
Enter the value of b:3
Enter the value of c:3
Yes
>> tutorial3_4
Enter the value of a:1
Enter the value of b:1
Enter the value of c:8
No
优化
a=input('Enter the value of a:');
b=input('Enter the value of b:');
c=input('Enter the value of c:');
% if a+b<=c
% fprintf('No\n');
% elseif a+c<=b
% fprintf('No\n');
% elseif b+c<=a
% fprintf('No\n');
% else
% fprintf('Yes\n');
% end
%同时选中后点击注释%,运行时直接跳过
if (a+b>c)&&(b+c>a)&&(a+c>b) %&&表示并且
fprintf('Yes\n');
else
fprintf('No\n')
end
Enter the value of a:7
Enter the value of b:8
Enter the value of c:9
Yes
判断二次方程是否有根
a=input('a=');
b=input('b=');
c=input('c=');
delta=b^2-4*a*c;
if delta>0 %如果△>0,则有两个解
fprintf('2 solutions\n');
elseif delta==0 %=表示赋值,==表示等于
fprintf('1 solutions\n');
else
fprintf('no solutions\n');
end
tutorial3-5
>> tutorial3_5
a=3
b=2
c=1
no solutions
>> tutorial3_5
a=1
b=-2
c=1
1 solutions
a=input('a=');
b=input('b=');
c=input('c=');
delta=b^2-4*a*c;
if delta>0 %如果△>0,则有两个解
x1=(-b+ sqrt(delta))/2*a; %sqrt表示根号
x2=(-b-sqrt(delta))/2*a;
fprintf('x1=%g\n',x1);
fprintf('x2=%g\n',x2);
elseif delta==0 %=表示赋值,==表示等于
x=(-b/2*a);
fprintf('x=%g\n',x);
else
fprintf('no solutions\n');
end
>> tutorial3_5
a=1
b=-2
c=1
x=1
>> tutorial3_5
a=1
b=1
c=-2
x1=1
x2=-2
B4 while循环
>> %fprintf()做输出控制
>> a=10;
>> fprintf('a=%g\n',a)
a=10
>> disp(a);
10
>> a
a =
10
>> 'apple'
ans =
apple
>> 'banana'
ans =
banana
>> 'orange'
ans =
orange
>> disp(['apple','banana','orange'])
applebananaorange
>> disp(['apple ','banana ','orange '])
apple banana orange
>> fr1='apple';
>> fe2='banana';
>> fr3='orange';
>> disp([fr1,fr2,fr3])
applebananaorange
>> disp([fr1,' ',fe2,' ',fr3]);
apple banana orange
>>
tutorial4-1
% 让用户输入一个数字num
% 这个数字num必须是1-100之间的数字
% 如果不在这个范围内,则输出‘Wrong Number'
% 如果数字在这个范围内,则输出这个数字的平方
num=input('Enter an interger between 1 and 100:')
if num >=1 && num <=100 %matlab中不可以用1 <= num <= 100表示,
%在matlab中,大于等于,小于等于只能用于两个数之间的比较
disp(num^2);
else
disp('Wrong Number');
end
>> tutorial4_1
Enter an interger between 1 and 100:6
num =
6
36
>> tutorial4_1
Enter an interger between 1 and 100:0
num =
0
Wrong Number
>>
% 让用户输入一个数字num
% 这个数字num必须是1-100之间的数字
% 如果不在这个范围内,则输出‘Wrong Number'
% 如果数字在这个范围内,则输出这个数字的平方
num=input('Enter an interger between 1 and 100:')
if num < 1 || num > 100 %||表示或者
disp('Wrong Number');
else
disp(num^2);
end
>> tutorial4_1
Enter an interger between 1 and 100:5
num =
5
25
>> tutorial4_1
Enter an interger between 1 and 100:-5
num =
-5
Wrong Number
循环
t=1;
while t < 5
disp(t);
t = t + 1;
end
>> tutorial4_2
1
2
3
4
t=1 disp(1)
t=2 disp(2)
t=3 disp(3)
t=4 disp(4)
t=5 end
t=1;
while t <= 5
disp(t);
t = t + 1;
end
>> tutorial4_2
1
2
3
4
5
输出1-10的所有奇数
t=1;
while t <= 10
disp(t);
t = t + 2;
end
>> tutorial4_2
1
3
5
7
9
t=1; %t是计数变量,计算次数
while t <= 5
disp('Hello');
t = t + 1;
end
>> tutorial4_2
Hello
Hello
Hello
Hello
Hello
t 本身没有参与到运算,只是做循环次数的控制,常用i表示,叫counter
i = 1;
s = 0;
while i <= 100
s = s + i;
i = i + 1;
end
i=1 s=0
s=0+1
i=2 s=0+1+2
i=3 s=0+1+2+3
i=4 s=0+1+2+3+4
·
·
·
i=100 s=0+1+2+...+100
% 1+2+3+4+...+100
i = 1;
s = 0;
while i <= 100
s = s + i;
i = i + 1;
end
disp(s);
>> tutorial4_3
5050
i = 1;
s = 0;
while i <= 100
s = s + i;
disp(s);
i = i + 1;
end
>> tutorial4_3
1
3
6
10
15
·
(逐个显示)
·
4851
4950
5050
B5 for 循环、步长
最大公约数
72 81
1 1
2 3
3 9
4 ·
8 ·
9 ·
· ·
81÷72=1······9
72÷9 =8······0
196 371
371÷196=1······175
196÷175=1······21
175÷21=8······7
21÷7=3······0
余数:mod(371,196):表示371除以196的余数
a表示除数,b表示被除数,r表示余数,商不带入运算,故不表示
a=input('a=');
b=input('b=');
r=mod(a,b);
while r~=0 %~=表示不等于≠
a=b;
b=r;
r=mod(a,b);
end
disp(b);
>> tutorial5_1
a=371
b=196
7
>> tutorial5_1
a=196
b=371
7
for 循环
for i = 1:5
disp(i);
end
>> tutorial5_2
1
2
3
4
5
for i = -2:5
disp(i);
end
>> tutorial5_2
-2
-1
0
1
2
3
4
5
for i = 2:-5
disp(i);
end
>> tutorial5_2
>>
逆序不输出
for i = 2:-1:-5
%加入步长:表示2到-5,每一次减一
disp(i);
end
>> tutorial5_2
2
1
0
-1
-2
-3
-4
-5
for i = -2:1:2
%加入步长:表示从2到-5,每一次减一
disp(i);
end
>> tutorial5_2
-2
-1
0
1
2
for i = -2:2
%步长为1可以省略
disp(i);
end
>> tutorial5_2
-2
-1
0
1
2
for i = -2:0.5:2
disp(i);
end
>> tutorial5_2
-2
-1.5000
-1
-0.5000
0
0.5000
1
1.5000
2
优化:while 函数和 if 循环
s=0;
for i = 1:100
s=s+i;
end
disp(s);
>> tutorial5_2
5050
分数
s=0;
for i =1:100
s=s+(1/i);
end
disp(s);
>> tutorial5_3
5.1874
s=0;
for i =1:100
if mod(i,2)==1
s=s+(1/i);
else
s=s-(1/i);
end
end
disp(s);
>> tutorial5_4
0.6882
######tutorial5_5
v=[5 7 9 10 13 3 2 1];
for i=v
disp(i);
end
>> tutorial5_5
5
7
9
10
13
3
2
1
求和
v=[5 7 9 10 13 3 2 1];
s=0;
for i=v
s=s+i;
end
disp(s);
disp(sum(v));
>> tutorial5_5
50
50
%for循环跟sum算出的结果一样,代码正确
B6 约数、质数
>> v=[1 2 3 4 5]
v =
1 2 3 4 5
>> sum(v)
ans =
15
>> mod(8,3)
ans =
2
>> tutorial5_5
50
50
>> %sum或mod就是一个function
最简单的Script
s=0;
for i =1:100
s=s+i;
end
disp(s);
>> tutorial6_1
5050
Function
function [ output_args ] = Untitled22( input_args )
%UNTITLED22 此处显示有关此函数的摘要
% 此处显示详细说明
end
function mysum()
s=0;
for i =1:100
s=s+i;
end
disp(s);
end
直接输入mysum
>> mysum()
5050
% n-input parameter / argument(参数)
function mysum(n)
s=0;
for i =1:n
s=s+i;
end
disp(s);
end
>> mysum()
输入参数的数目不足。
出错 mysum (line 3)
for i =1:n
n=10
function mysum(n)
s=0;
for i =1:n
s=s+i;
end
disp(s);
end
>> mysum(10)
55
function mysum2( a,b )
s=0;
for i=a:b;
s=s+i;
end
disp(s);
end
>> mysum2(4,10)
49
>> v1=[6 7 8 9]
v1 =
6 7 8 9
>> v2=[1 3 5 7 9]
v2 =
1 3 5 7 9
>> s1=sum(v1)
s1 =
30
>> s2=sum(v2)
s2 =
25
>> s=s1+s2
s =
55
>> s1=mysum2(4,9)
错误使用 mysum2
输出参数太多。
>> s1=sum(v1)
s1 =
30
function result = mysum3( a,b )
s=0;
for i=a:b;
s=s+i;
end
result=(s); %return 返回值
end
>> s1=mysum3(4,9) %s1对应为result的值
s1 =
39
>> s1=mysum3(4,7)
s1 =
22
>> s2=mysum3(11,15)
s2 =
65
>> s1+s2
ans =
87
##约数(能被整除)
8:1 2 4 8 (8有4个约数)
function result = count_factors( n )
count=0;
for i=1:n
if mod(n,i)==0
count=count+1;
end
end
result=count;
end
>> count_factors( 5 )
ans =
2
>> count_factors( 19 )
ans =
2
>> count_factors( 22 )
ans =
4
>> count_factors( 24 )
ans =
8
>> count_factors( 256 )
ans =
9
判断 n 是不是质数
质数(prime number)
variable scope(作用域)
不同function中的result(相同名字的量)不会互相影响
function result = is_prime( n )
count=count_factors(n);
if count==2
result=1; %return=1表示是质数
else
result=0; %return=0表示不是质数
end
end
>> is_prime( 11)
ans =
1 %是质数
>> is_prime( 10 )
ans =
0 %不是质数
>> is_prime( 3 )
ans =
1 %是质数
>> is_prime( 457 )
ans =
1 %是质数
>> is_prime( 458 )
ans =
0 %不是质数
%列出1—1000之间的所有质数
for i=1:1000
if is_prime(i)==1
disp(i);
end
end
>> tutorial6_2
2
3
5
7
11
13
`
`
(逐个显示)
`
`
977
983
991
997
>>
B7 plot画图
####画图
######直线
>> x=[1 2 3];
>> y=[4 5 6];
>> plot(x,y)
######抛物线
y=x^2
>> % x=-3:3; 表示x=[-3 -2 -1 0 1 2 3]
>> %即-3到3的所有整数 :表示步长
>> x=-3:3;
>> y=x.*x; %点乘
>> plot(x,y);
######优化
>> x=-3:0.1:3;
>> y=x.*x; % 或y=x.^2
>> plot(x,y)
######同时画多个函数
>> x=-3:0.1:3;
>> y1=x.^2;
>> y2=x.^3;
>> plot(x,y1,'green',x,y2,'black')
>> x=-3:0.1:3;
>> y1=x.^2;
>> y2=x.^3;
>> plot(x,y1,'green',x,y2,'black')
>> plot(x,y1,'green-o')
######关键点用圈表示出来
######使横纵坐标单位长度相等
>> x=-3:0.1:3;
>> y1=x.^2;
>> y2=x.^3;
>> plot(x,y1,'green',x,y2,'black')
>> plot(x,y1,'green-o')
>> axis equal
######直方图
>> y=[75 91 105 123.5 131 150 179 203 226 249 281.5];
>> bar(y)
######更改标签
>> y=[75 91 105 123.5 131 150 179 203 226 249 281.5];
>> bar(y)
>> x=2000:2010;
>> bar(x,y)
######三维图像画法
先在平面内画出x,y轴
>> theta=0:pi/50:6*pi;
>> x=cos(theta);
>> y=sin(theta);
>> plot(x,y)
plot3表示画3D图像
>> theta=0:pi/50:6*pi;
>> x=cos(theta);
>> y=sin(theta);
>> plot(x,y)
>> z=0:300;
>> plot3(x,y,z)
B8 subplot函数、surf 画图、meshgrid函数
x1=-5:0.1:5;
y1=x1.^2;
plot(x1,y1);
x1=-5:0.1:5;
y1=x1.^2;
plot(x1,y1);
x2=-5:0.1:5;
y2=x2.^3;
plot(x2,y2);
x1=-5:0.1:5;
y1=x1.^2;
plot(x1,y1);
hold on
x2=-5:0.1:5;
y2=x2.^3;
plot(x2,y2);
添加方格
x1=-5:0.1:5;
y1=x1.^2;
plot(x1,y1);
hold on
x2=-5:0.1:5;
y2=x2.^3;
plot(x2,y2);
grid on;
添加标题
x1=-5:0.1:5;
y1=x1.^2;
plot(x1,y1);
hold on
x2=-5:0.1:5;
y2=x2.^3;
plot(x2,y2);
grid on;
title('x^2 vs x^3');
添加坐标轴标签
x1=-5:0.1:5;
y1=x1.^2;
plot(x1,y1);
hold on
x2=-5:0.1:5;
y2=x2.^3;
plot(x2,y2);
grid on;
title('x^2 vs x^3');
xlabel('x-axis');
ylabel('y-axis');
######subplot函数
subplot(2,3,1)表示2行3列,在1中画的图
x=-4:0.1:4;
y1=sin(x);
y2=sin(2.*x);
y3=sin(3.*x);
y4=sin(4.*x);
subplot(2,2,1);
plot(x,y1);
x=-4:0.1:4;
y1=sin(x);
y2=sin(2.*x);
y3=sin(3.*x);
y4=sin(4.*x);
subplot(2,2,1);
plot(x,y1);
subplot(2,2,2);
plot(x,y2);
subplot(2,2,3);
plot(x,y3);
subplot(2,2,4);
plot(x,y4);
x=-4:0.1:4;
y1=sin(x);
y2=sin(2.*x);
y3=sin(3.*x);
y4=sin(4.*x);
subplot(2,2,1);
plot(x,y1);
title('y=sin(x)');
subplot(2,2,2);
plot(x,y2);
title('y=sin(2x)');
subplot(2,2,3);
plot(x,y3);
title('y=sin(3x)');
subplot(2,2,4);
plot(x,y4);
title('y=sin(4x)');
x=-4:0.1:4;
y1=cos(x);
y2=cos(2.*x);
y3=cos(4.*x);
subplot(2,2,1);
plot(x,y1);
title('y=cos(x)');
subplot(2,2,2);
plot(x,y2);
title('y=cos(2x)');
subplot(2,2,3);
plot(x,y3);
title('y=cos(3x)');
x=-4:0.1:4;
y1=cos(x);
y2=cos(2.*x);
y3=cos(4.*x);
subplot(2,2,1);
plot(x,y1);
title('y=cos(x)');
subplot(2,2,2);
plot(x,y2);
title('y=cos(2x)');
subplot(2,2,[3,4]);
plot(x,y3);
title('y=cos(3x)');
######surf 画三维立体图
sueface缩写surf
surf(x,y,z)
>> x=-3:3;
>> y=-3:3;
>> x
x =
-3 -2 -1 0 1 2 3
>> y
y =
-3 -2 -1 0 1 2 3
>> z=x.^2+y.^2;
>> z
z =
18 8 2 0 2 8 18
>> [X,Y]=meshgrid(x,y);
>> X
X =
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 1 2 3
>> Y
Y =
-3 -3 -3 -3 -3 -3 -3
-2 -2 -2 -2 -2 -2 -2
-1 -1 -1 -1 -1 -1 -1
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
>> Z=X.^2+Y.^2;
>> Z
Z =
18 13 10 9 10 13 18
13 8 5 4 5 8 13
10 5 2 1 2 5 10
9 4 1 0 1 4 9
10 5 2 1 2 5 10
13 8 5 4 5 8 13
18 13 10 9 10 13 18
>> surf(X,Y,Z)
######精细处理
>> x=-3:0.1:3;
>> y=-3:0.1:3;
>> [X,Y]=meshgrid(x,y);
>> Z=X.^2+Y.^2;
>> surf(X,Y,Z)
B9 动画
X=-pi:0.1:pi;
Y=sin(X);
plot(X,Y);
X=-2*pi:0.1:2*pi;
Y=sin(X);
plot(X,Y);
######运行100次
X=-2*pi:0.1:2*pi;
Y=sin(X);
h=plot(X,Y);
for i=1:100
X=X+0.1;
Y=sin(X);
set(h,'XData',X,'YData',Y)
drawnow;
end
######运行1000次
X=-2*pi:0.1:2*pi;
Y=sin(X);
h=plot(X,Y);
for i=1:1000
X=X+0.1;
Y=sin(X);
set(h,'XData',X,'YData',Y)
drawnow;
end
######不断循环(暂时错误)
X=-2*pi:0.1:2*pi;
Y=sin(X);
h=plot(X,Y);
while ture
X=X+0.1;
Y=sin(X);
set(h,'XData',X,'YData',Y)
drawnow;
end
theta=-10*pi:0.1:10*pi;
X=cos(theta);
Y=sin(theta);
Z=theta;
h=plot3(X,Y,Z);
######变换
theta=-10*pi:0.1:10*pi;
X=cos(theta);
Y=sin(theta);
Z=theta;
h=plot3(X,Y,Z);
for i=1:100
Z=0.98*Z;
set(h,'XData',X,'YData',Y,'ZData',Z);
drawnow
end
######固定坐标轴压缩
使用axis([])函数[]内输入x,y,z的取值范围
-10pi约等于40
theta=-10*pi:0.1:10*pi;
X=cos(theta);
Y=sin(theta);
Z=theta;
h=plot3(X,Y,Z);
axis([-1,1,-1,1,-40,40])
for i=1:100
Z=0.98*Z;
set(h,'XData',X,'YData',Y,'ZData',Z);
drawnow
end
######拉伸
先压缩后拉伸
theta=-10*pi:0.1:10*pi;
X=cos(theta);
Y=sin(theta);
Z=theta;
h=plot3(X,Y,Z);
axis([-1,1,-1,1,-40,40])
for i=1:100
Z=0.98*Z;
set(h,'XData',X,'YData',Y,'ZData',Z);
drawnow
end
for i=1:100
Z=Z/0.98;
set(h,'XData',X,'YData',Y,'ZData',Z);
drawnow
end
######重复循环
while函数
theta=-10*pi:0.1:10*pi;
X=cos(theta);
Y=sin(theta);
Z=theta;
h=plot3(X,Y,Z);
axis([-1,1,-1,1,-40,40])
while true
for i=1:100
Z=0.98*Z;
set(h,'XData',X,'YData',Y,'ZData',Z);
drawnow
end
for i=1:100
Z=Z/0.98;
set(h,'XData',X,'YData',Y,'ZData',Z);
drawnow
end
end
######更改圆的半径
axis函数
theta=-10*pi:0.1:10*pi;
X=cos(theta);
Y=sin(theta);
Z=theta;
h=plot3(X,Y,Z);
axis([-2,2,-2,2,-40,40])
while true
for i=1:100
Z=0.98*Z;
set(h,'XData',X,'YData',Y,'ZData',Z);
drawnow
end
for i=1:100
Z=Z/0.98;
set(h,'XData',X,'YData',Y,'ZData',Z);
drawnow
end
end
######画圆
t=0:0.1:2*pi;
X=cos(t);
Y=sin(t);
plot(X,Y);
######使横纵坐标相等
有缺口:0.1无法到达2*pi
t=0:0.1:2*pi;
X=cos(t);
Y=sin(t);
plot(X,Y);
axis equal;
修改分段数量
t=0:pi/50:2*pi;
X=cos(t);
Y=sin(t);
plot(X,Y);
axis equal;
######画半径
从(0,0)出发的,长为1的线段
即x=(0,1),y=(0,0)
t=0:pi/50:2*pi;
X=cos(t);
Y=sin(t);
plot(X,Y);
axis equal;
lineX=[0,1];
lineY=[0,0];
plot(lineX,lineY);
######hold on
t=0:pi/50:2*pi;
X=cos(t);
Y=sin(t);
plot(X,Y);
hold on;
axis equal;
lineX=[0,1];
lineY=[0,0];
h=plot(lineX,lineY);
######半径循环转动1000次
t=0:pi/50:2*pi;
X=cos(t);
Y=sin(t);
plot(X,Y);
hold on;
axis equal;
lineX=[0,1];
lineY=[0,0];
h=plot(lineX,lineY);
theta=0;
for i=1:1000;
theta=theta+0.1;
lineX(2)=cos(theta);
lineY(2)=sin(theta);
set(h,'XData',lineX,'YData',lineY);
drawnow;
end
######减速
theta=theta+0.05;
t=0:pi/50:2*pi;
X=cos(t);
Y=sin(t);
plot(X,Y);
hold on;
axis equal;
lineX=[0,1];
lineY=[0,0];
h=plot(lineX,lineY);
theta=0;
for i=1:1000;
theta=theta+0.05;
lineX(2)=cos(theta);
lineY(2)=sin(theta);
set(h,'XData',lineX,'YData',lineY);
drawnow;
end
for循环一定次数,while无限循环
music
>> x=linspace(0,2*pi,100);
>> y=sin(x);
>> plot(x,y)
采样率:Fs
matlab中1s中播放8192个点,区间在1000到384000之间才可以播放
本例中1s播放100个点
>> Fs=8192;
>> x=linspace(0,2*pi,Fs);
>> y=sin(x);
>> plot(x,y);
>> sound(y,Fs);
>> sound(y,Fs);
>> sound(y,Fs);
>> Fs=8192;
x=linspace(0,2*pi,Fs);
y=sin(x);
plot(x,y);
>> x=linspace(0,2*pi,Fs);
>> y=sin(x);
>> plot(x,y);
>> plot(x,y);
>> plot(x,y);
>> y=sin(2*x);
>> plot(x,y);
>> y=sin(3*x)
>> plot(x,y);
标准A是440Hz
>> x=linspace(0,2*pi,Fs);
>> y=sin(x);
>> plot(x,y);
>> y=sin(2*x);
>> plot(x,y);
>> y=sin(3*x);
>> plot(x,y);
>> y=sin(440*x);
>> plot(x,y);
>>
523;587;659;698;783;880;988
>> x=linspace(0,2*pi,Fs);
>> y=sin(x);
>> plot(x,y);
>> y=sin(2*x);
>> plot(x,y);
>> y=sin(3*x);
>> plot(x,y);
>> y=sin(440*x);
>> plot(x,y);
>> sound(y,Fs)
>> sound(y,Fs)
>> Fs1=8192;
>> Fs2=20000;
>> x1=linspace(0,2*pi,Fs1);
>> x2=linspace(0,2*pi,Fs2);
>> Fs1=8192;
Fs2=20000;
x1=linspace(0,2*pi,Fs1);
x2=linspace(0,2*pi,Fs2);
>> y1=sin(440*x1);
>> y2=sin(440*x2);
>> sound(y1,Fs1);
>> sound(y2,Fs2);
>>
>> Fs=8192;
>> x=linspace(0,2*pi,Fs);
>> y1=sin(440*x);
>> y2=sin(500*x);
>> y3=sin(650*x);
>>
>> sound(y1,Fs);
>> sound(y2,Fs);
>> sound(y3,Fs);
>> freqs=[523 587 659 698 783 880 988];
>> y1=sin(freqs(1)*x);
>> y2=sin(freqs(2)*x);
>> y1=sin(freqs(1)*x);
>> y1=sin(freqs(1)*x);
>> y1=sin(freqs(1)*x);
>> y1=sin(freqs(1)*x);
>> y2=sin(freqs(2)*x);
>> y3=sin(freqs(3)*x);
>> y4=sin(freqs(4)*x);
>> y5=sin(freqs(5)*x);
>> y=[y1.y2,y3,y4,y5];
结构体内容引用自非结构体数组对象。
>> freqs=[523 587 659 698 783 880 988];
>> y1=sin(freqs(1)*x);
>> y2=sin(freqs(2)*x);
>> y3=sin(freqs(3)*x);
>> y4=sin(freqs(4)*x);
>> y5=sin(freqs(5)*x);
>> y=[y1.y2,y3,y4,y5];
结构体内容引用自非结构体数组对象。
>> freqs=[523 587 659 698 783 880 988];
>> y1=sin(freqs(1)*x);
>> y2=sin(freqs(2)*x);
>> y3=sin(freqs(3)*x);
>> y4=sin(freqs(4)*x);
>> y5=sin(freqs(5)*x);
>> y=[y1,2,y3,y4,y5];
>> freqs=[523 587 659 698 783 880 988];
>> freqs=[523,587,659,698,783,880,988];
>> y1=sin(freqs(1)*x);
>> y2=sin(freqs(2)*x);
>> y3=sin(freqs(3)*x);
>> y4=sin(freqs(4)*x);
>> y5=sin(freqs(5)*x);
>> y=[y1,2,y3,y4,y5];
>> sound(,Fs)
sound(,Fs)
↑
错误: 表达式或语句不正确--可能 (、{ 或 [ 不对称。
>> sound(y,Fs)
>> music
错误: 文件:music.m 行:6 列:18
圆括号或方括号不对称或异常。
>> music
错误: 文件:music.m 行:6 列:18
圆括号或方括号不对称或异常。
>> music
>>
Fs=8192;
x=linspace(0,2*pi,Fs);
freqs=[523,587,659,698,783,880,988];
y1=sin(freqs(1)*x);
y2=sin(freqs(1)*x);
y3=sin(freqs(5)*x);
y4=sin(freqs(5)*x);
y5=sin(freqs(6)*x);
y6=sin(freqs(6)*x);
y7=sin(freqs(5)*x);
y8=sin(freqs(5)*x);
y=[y1,y2,y3,y4,y5,y6,y7,y8];
plot(y),
% sound(y,Fs);
Fs=8192;
x=linspace(0,2*pi,Fs);
freqs=[523,587,659,698,783,880,988];
y1=sin(freqs(1)*x) .* (1-x/(2*pi));
y2=sin(freqs(1)*x) .* (1-x/(2*pi));
y3=sin(freqs(5)*x) .* (1-x/(2*pi));
y4=sin(freqs(5)*x) .* (1-x/(2*pi));
y5=sin(freqs(6)*x) .* (1-x/(2*pi));
y6=sin(freqs(6)*x) .* (1-x/(2*pi));
y7=sin(freqs(5)*x);
y8=sin(freqs(5)*x);
y=[y1,y2,y3,y4,y5,y6,y7,y8];
plot(y),
% sound(y,Fs);
Fs=8192;
x=linspace(0,2*pi,Fs);
freqs=[523,587,659,698,783,880,988];
y1=sin(freqs(1)*x) .* (1-x/(2*pi));
y2=sin(freqs(1)*x) .* (1-x/(2*pi));
y3=sin(freqs(5)*x) .* (1-x/(2*pi));
y4=sin(freqs(5)*x) .* (1-x/(2*pi));
y5=sin(freqs(6)*x) .* (1-x/(2*pi));
y6=sin(freqs(6)*x) .* (1-x/(2*pi));
y7=sin(freqs(5)*x);
y8=sin(freqs(5)*x);
y=[y1,y2,y3,y4,y5,y6,y7,y8];
%plot(y),
sound(y,Fs);
Fs=8192;
x =linspace(0,2*pi,Fs);
x2=linspace(0,2*pi*2,Fs*2);
freqs=[523,587,659,698,783,880,988];
y1=sin(freqs(1)*x) .* (1-x/(2*pi));
y2=sin(freqs(1)*x) .* (1-x/(2*pi));
y3=sin(freqs(5)*x) .* (1-x/(2*pi));
y4=sin(freqs(5)*x) .* (1-x/(2*pi));
y5=sin(freqs(6)*x) .* (1-x/(2*pi));
y6=sin(freqs(6)*x) .* (1-x/(2*pi));
y7=sin(freqs(5)*x2) .* (1-x2/(2*pi));
y=[y1,y2,y3,y4,y5,y6,y7,y8];
plot(y),
%sound(y,Fs);
Fs=8192;
x =linspace(0,2*pi,Fs);
x2=linspace(0,2*pi*2,Fs*2);
freqs=[523,587,659,698,783,880,988];
y1=sin(freqs(1)*x) .* (1-x/(2*pi));
y2=sin(freqs(1)*x) .* (1-x/(2*pi));
y3=sin(freqs(5)*x) .* (1-x/(2*pi));
y4=sin(freqs(5)*x) .* (1-x/(2*pi));
y5=sin(freqs(6)*x) .* (1-x/(2*pi));
y6=sin(freqs(6)*x) .* (1-x/(2*pi));
y7=sin(freqs(5)*x2) .* (1-x2/(4*pi));
y=[y1,y2,y3,y4,y5,y6,y7];
plot(y),
%sound(y,Fs);
单纯形法
求解线性规划问题
f=-[2 1 -1];
A=[1 4 -1; 2 -2 1];
b=[4;12];
Aeq=[1 1 2];
beq=6;
lb=zeros(3,1);
x0=[0; 0; 0];
options=optimset('LargeScale', 'on', 'Display', 'iter', 'TolFun', 1e-3);
[x,fval,exitflag,output,lambda]=linprog(f,A,b,Aeq,beq,lb,[],x0,options)
lambda.lower
%运行结果
>> linprog1_1
The interior-point-legacy algorithm uses a built-in starting point;
ignoring supplied X0.
Residuals: Primal Dual Duality Total
Infeas Infeas Gap Rel
A*x-b A'*y+z-f x'*z Error
---------------------------------------------------
Iter 0: 6.61e+02 7.16e+00 1.35e+03 2.00e+02
Iter 1: 4.29e+01 6.66e-16 1.28e+02 3.06e+00
Iter 2: 5.10e-15 8.95e-16 2.27e+01 1.13e+00
Iter 3: 1.26e-15 8.91e-15 1.10e+01 1.06e+00
Iter 4: 4.03e-13 9.16e-16 2.62e+00 2.42e-01
Iter 5: 1.16e-14 1.95e-14 1.44e-01 1.65e-02
Iter 6: 8.88e-16 1.09e-15 1.20e-05 1.38e-06
Optimization terminated.
x =
4.6667
0.0000
0.6667
fval =
-8.6667
exitflag =
1
output =
iterations: 6
algorithm: 'interior-point-legacy'
cgiterations: 0
message: 'Optimization terminated.'
constrviolation: 0
firstorderopt: 4.3452e-06
lambda =
ineqlin: [2x1 double]
eqlin: 0.3333
upper: [3x1 double]
lower: [3x1 double]
ans =
0.0000
6.0000
0.0000
x返回最优解;
fval返回目标函数值;
A和b对应不等式约束Ax≤b;
Aeq和beq对应等式约束Ax=b;
LB和UB分别是变量x的下界和上界;
x0为x的初始值;
options为控制参数;
exitflag返回算法停止的原因:
1表示成功找到最优解,
0表示达到最大迭代次数,不能继续寻找最优解,
<0表示优化失败
(-2未找到可行解,
-3问题没有定义边界,
-4 NaN存在导致算法退出,
-5原始对偶问题没有可行解
,-7算法搜索方向存在问题);
output返回algorithm采用的算法(大中小型),迭代次数等优化信息;
lambda返回最优解x处的拉格朗日乘子的一些参数。
options参数设置:
1)options=optimset(‘optimfun’)
若已有设置好的参数项设置,直接使用其名称即可;
2)opts=optimset(‘param1’,
value1, ‘param2’, value2, …)
创建一个名为opts的参数设置,分别指定参数值,未指定的保持默认。例如,要设置使用大型算法、显示每次迭代、允许误差为10
—————————————
https://zhuanlan.zhihu.com/p/61466360