Today, I slept 14 hours, really comfortable, long time not so cool, hey (* ヘ | · ∀ · | Techno * ~ ●
Structured programming and self-defined function
if else
a=3; if rem(a,2)==0 disp('a is even') else disp('a is odd') end
switch
input_num=1; switch input_num case -1 disp('negative 1'); case 0 disp('zero'); otherwise disp('other value'); end
while
n=1; while prod(1:n)<1e100 n=n+1; end
for
for variable=start:increment:end
commands
end
for n=1:2:10 a(n)=2^n; end disp(a);
clear all clear all historical data
close all close all data
clc clear screen
... Wrap
terminate the program ctrl + c
edit (which ( 'mean.m')) of built-in functions
>> edit('mean.m');
Custom Functions
function x=freebody(x0,v0,t) x=x0+v0.*t+1/2*9.8*t.*t;
Point multiply and multiply the difference
A matrix multiplication operation, dot matrix multiplication is in the same position of the element
In the custom function can be calculated using the plurality of sets of dot data at the same
>> Freebody ([0.9], [2.7], [2.3]) ans = 23.6000 74.1000
Establishing a plurality of input and output functions of
function [a,F]=freebody(v2,v1,t2,t1,m) a=(v2-v1)./(t2-t1); F=m.*a; [a F]=freebody(20,10,5,4,1) a = 10 F = 10
function handle
Basic drawing
line types
solid line -
dashed line --
dash-dotted line -.
dotted line :
hold on x=0:pi/10:2*pi; y1=sin(x); y2=x; plot(x,y1,':or',x,y2,'--+g'); hold off
ON HOLD X = 0: 0.5: * PI. 4; Y = SiN (X); HH = Tan (X); H = Y / HH;. W =. 1 ./ (. 1 + exp (the -X-)); G = . (. 1 / (2 * PI * 2) ^ 0.5) * exp ((-. 1 * (2 * X-PI) ^ 2) ./ (2 * 2 ^ 2)..); Plot (X, Y, 'BD -', X, H, 'GP:', X, W, 'RO -', X, G, 'C ^ -'); % was added identification legend ( 'sin (x)' , 'cos (x ) ',' the Sigmoid ',' GAUSS function '); % add a title and coordinates title (' title '); the xlabel (' T = 0to2 \ PI '); ylabel (' value '); HOLD OFF
text () function to specify the position of the display character string in the drawing is available on.
Annotation () to add comments, arrows, text boxes, etc., for the default coordinate normalized coordinates 0-1
hold on x=linspace(0,3); y=x.^2.*sin(x); plot(x,y); line([2,2],[0,2^2*sin(2)]); %积分符号 \int 下标_{0} 上标{1} str='$$ \int_{0}^{2}\sin(x) dx $$'; %以LaTeX表示法表示str,0.25表示x的坐标,2.5表示y的坐标 text(0.25,2.5,str,'Interpreter','latex'); %[0.32,0.5]是代表箭头在x坐标的位置,[0.6,0.4]是代表箭头在y坐标的位置,都要归一化,x乘3,y乘4 annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]); hold off
get() 获得句柄handle 详情https://ww2.mathworks.cn/help/matlab/ref/get.html
gca() return the handle of the "current" axes
gcf() return the handle the "current" figure
hold on x=linspace(0,2*pi,1000); y=sin(x); plot(x,y); h=plot(x,y); get(h); hold off
set(句柄,属性名1,属性值1,属性名2,属性值2,…) 设置句柄handle
set(gca,'Xlim',[0,pi]);
除了上面的方法可以设置handle,下面的方法也可以
xlim([0,2*pi]); ylim([-1.2,1.2]);
%改变坐标的大小 set(gca,'FontSize',25); %tick 设置几个刻度的意思 set(gca,'XTick',0:pi/2:2*pi); set(gca,'XTickLabel',0:90:360); %gca是针对坐标轴的改变 %改变线的样式 set(线的名称;样式;目标值,···); set(h,'LineStyle','-.','LineWidth',7.0,'Color','g');
figure('Position',[left,bottom,width,height]);
多张图画在一张图上
只有一个figure subplot(row,column,1);
x=-10:0.1:10; y1=x.^2-8; y2=exp(x); %可以表示写position,也可以不写 figure('Position',[300,100,500,500]),plot(x,y1); figure,plot(x,y2);
t=0:0.1:2*pi; x=3*sin(t)./tan(t); y=sin(t); subplot(2,2,1);plot(x,y);axis normal; subplot(2,2,2);plot(x,y);axis square; %axis equal x轴上一格距离大小与y轴上一格距离大小一致 subplot(2,2,3);plot(x,y);axis equal; subplot(2,2,4);plot(x,y);axis equal tight;
axis off 关闭最后画的一个图的坐标
axis on 打开最后画的一个图的坐标
box off 关闭最后画的一个图的上边线和右边线
box on 打开最后画的一个图的上边线和右边线
grid off 关闭最后一个画的图的栅格
grid on 打开最后一个画的图的栅格
>>box off >> grid on
进阶绘图
plotyy() 双y轴函数
hold on x=0:0.01:20; y1=200*exp(-0.05*x).*sin(x); y2=0.8*exp(-0.5*x).*sin(10*x); %下面的式子返回三个参数,ax是坐标轴的句柄,ax(1)是左边的纵轴,ax(2)时右边的纵轴 [ax,h1,h2]=plotyy(x,y1,x,y2); set(get(ax(1),'Ylabel'),'String','Left Y-axis'); set(get(ax(2),'Ylabel'),'String','Right Y-axis'); title('Labeling plotyy'); set(h1,'LineStyle','--'); set(h2,'LineStyle',':'); plotyy(x,y1,x,y2); hold off
randn()是均值为0方差为1的正态分布
hist(y,m) m是一个标量,表明使用m个箱子
hold on y=randn(1,1000); subplot(2,1,1); hist(y,10); title('Bins=10'); subplot(2,1,2); hist(y,50); title('Bins=50'); hold off
bar
hold on x=[1 2 5 4 8]; y=[x;1:5]; subplot(1,3,1);bar(x);title('A bargraph of vector x'); subplot(1,3,2);bar(y);title('A bargraph of vector y'); subplot(1,3,3);bar3(x);title('A 3D bargraph'); hold off
详情https://www.mathworks.com/help/matlab/ref/barh.html
pie chart
hold on a=[10 5 20 30]; subplot(1,3,1);pie(a); subplot(1,3,2);pie(a,[0,0,0,1]); subplot(1,3,3);pie3(a,[0,0,0,1]); hold off
polar chart
hold on x=1:100; theta=x/10;r=log10(x); subplot(1,4,1);polar(theta,r); theta=linspace(0,2*pi);r=sin(4*theta); subplot(1,4,2);polar(theta,r); theta=linspace(0,2*pi,6);r=ones(1,length(theta)); subplot(1,4,3);polar(theta,r); theta=linspace(0,2*pi);r=1-sin(theta); subplot(1,4,4);polar(theta,r); hold off