0%

matlab入门笔记

一、脚本编写

1、语句末尾加分号,屏蔽输出,但是变量依然存在

1
2
x = 5 //有输出
x = 5;//屏蔽输出,运行脚本不会在命令行窗口打印x和x的值

2、输出
disp会自动加上一个回车,不需要像fprintf一样再手动地加上一个\n了。

1
2
3
4
5
6
7
8
9
10
11
12
fprintf('Fahrenheit = %f', F);//F是要输出的变量名
fprintf('%f + %f = %f\n', x,y,x+y);//6.000000 + 7.000000 = 13.000000
fprintf('%g + %g = %g\n', x,y,x+y);//6 + 7 = 13

a = 10;
disp(a);//看一下变量a的值,是display的简写
disp(['apple ','banana ','orange']);//一定要有中括号
disp('Wrong Number');//这个可以没有中括号
fr1 = 'apple';
fr2 = 'banana';
fr3 = 'orange';
disp([fr1,' ',fr2,' ',fr3]);//注意空格的特殊加法

3、if语句

1
2
3
4
5
6
7
8
num = input('Please enter a number');
if num>0
fprintf('Positive\n');
elseif num<0
fprintf('Negative\n');
else
fprintf('num = 0\n');
end

4、逻辑与

1
2
3
4
5
6
7
8
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)&&(b + c > a)&&(a + c > b)
fprintf('Yes\n');
else
fprintf('No\n');
end

5、根号、平方和乘号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a = input('Enter the value of a: ');
b = input('Enter the value of b: ');
c = input('Enter the value of c: ');
delta = b^2 - 4*a*c;
if delta > 0
x1 = (-b+sqrt(delta))/(2*a);
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

6、循环
循环大致分成两种:while和for。
第一种while

1
2
3
4
5
t = 1;
while t<5//t为计数变量(counter)
disp(t);
t = t+1;
end

第二种for

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for i = 1:5//默认情况步长为1
disp(i)
end

for i = 5:-1:-5//修改步长为-1
disp(i)
end

for i = 1:0.5:5//步长可以不是整数
disp(i)
end

v = [5 7 9 10 13 3 2 1];//for循环用于打印向量

for i = v
disp(i);
end
disp(sum(v));//可以使用sum函数求出向量的和

7、求余数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mod(a,b);//means a%b;

//辗转相除法实现
a = input('a = ');
b = input('b = ');

r = mod(a,b);
while r~=0//这里相当于r!=0
a = b;
b = r;
r = mod(a,b);
end

disp(b);

8、函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//无参数无返回值
function function1()//函数名必须和新建的function文件同名
s = 0;
for i = 1:100
s = s + i;
end
disp(s);
end

//有一个参数无返回值
function function1(n)
s = 0;
for i = 1:n
s = s + i;
end
disp(s);
end

//有参数有返回值
function result = function1(a,b)
s = 0;
for i = a:b
s = s + i;
end
result = s;% return
end

二、画图

1、画函数线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//折线图
x = -3:3;//相当于x = [-3,-2,-1,0,1,2,3];
y = x.*x;//y = x*x;
plot(x,y);

//改进版,更多数据点,曲线更加光滑
x = -3:0.1:3;//设置步长,增加数据精度
y = x.^2;//y = x*x;
plot(x,y);

//一次画两条线,不同颜色
plot(x,y1,'green',x,y2,'black');

//显示关键点
plot(x,y1,'green-o');

//将横轴和纵轴的单位长度调为相同
axis equal;

2、直方图

1
2
3
4
5
6
7
8
//只有y轴数据
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y);

//有x,y两组数据
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
x = 2000:2010;
bar(x,y);

3、三维图像(线)

1
2
3
4
5
theta = 0:pi/50:6*pi;
x = cos(theta);
y = sin(theta);
z = 0:300;
plot3(x,y,z);

4、画图小技巧

1
2
3
4
5
6
7
8
9
10
11
x1 = -5:0.1:5;
y1 = x1.^2;
plot(x1,y1);
hold on;//使用hold on将两张图画在同一个坐标系中
x2 = -5:0.1:5;
y2 = x2.^3;
plot(x2,y2);
grid on;//显示背景网格
title('x^2 vs x^3');//添加图片标题
xlabel('x-axis');//x轴上的标签
ylabel('y-axis');//y轴上的标签

subplot(行数,列数,当前操作的页面编号);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//排版四个图
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);
y4 = sin(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(4x)');

5、三维图像(面)

1
2
3
4
5
x = -3:0.1:3;
y = -3:0.1:3;
[X,Y] = meshgrid(x,y);% 将x和y组合成具体的点
Z = X.^2+Y.^2;
surf(X,Y,Z);

6、正弦动画

1
2
3
4
5
6
7
8
9
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);% 重新计算新的y值
set(h,'XData',x,'YData',y);% 重新设置x,y的值
drawnow;% 重新绘制一遍图
end

7、弹簧动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

8、钟表动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
t = 0:pi/50:2*pi;
X = cos(t);
Y = sin(t);
plot(X,Y);
hold on;
axis equal;
theta = 0;
lineX = [0, 1];
lineY = [0, 0];
h = plot(lineX,lineY);
for i = 1:1000
theta = theta+0.1;
lineX(2) = cos(theta);
lineY(2) = sin(theta);
set(h,'XData',lineX,'YData',lineY);
drawnow;
end