Matlab 基础笔记
2020-08-15 15:05
1651 查看
绘图
%改变绘图风格 plot(x,y,'str') %Data markers . * x 0 + s d p v ^ < > H %Line types - -- -. : %Colors k b c g m r w y
%增加图标 legend('L1',....) title() xlabel() ylabel()
%增加文本及箭头 x = linspace(0,3); y = x.^2.*sin(x); plot(x,y); line([2,2],[0,2^2*sin(2)]); str = '.....'; text(0.25,2.5,str,'Interpreter','latex'); annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);
% 查看指令 h = plot(x,y); get(h) get(gca) get(gcf) %修改图片 set(gca,'XLim',[0,2*pi]) xlim([0,2*pi])
%同一画布绘图 hold on plot(x,y); plot(a,b); hold off plotyy(x,y1,x,y2) %不同画布绘图 figure,plot(x,y1); figure,plot(x,y2); %制定位置 figure('Position',[left,bottm,width,height) %同一画布绘制多图 %m行n列 subplot(m,n,1); %坐标开关 axis on/off %网格开关 grid on/off %坐标轴上线及右线开关 box on/off %图片保存 saveas(gcf,'<filename>','<formattype>')
%对数绘图 x = logspace(-1,1,100); y = x.^2; subplot(2,2,1); plot(x,y); title('plot'); subplot(2,2,2); semilogx(x,y);%x坐标取对数 title('semilogx'); subplot(2,2,3); semilogy(x,y);%y坐标取对数 title('semilogy'); subplot(2,2,4); loglog(x,y);%都取对数 title('loglog');
%统计图表 y = randn(1,1000); subplot(2,1,1); hist(y,10); title('Bins = 10'); subplot(2,1,2); hist(y,50); title('Bins = 50')
%柱状图 x = [1 2 5 4 8]; y = [x;1:5]; subplot(1,3,1);bar(x);%bar(y,'stacked') 叠状 subplot(1,3,2);bar(y);%barh(y) 竖着 subplot(1,3,3);bar3(y);
%饼状图 a = [10 5 20 30]; subplot(1,3,1);pie(a); subplot(1,3,2);pie(a,[0,0,0,1]);%最后一份其余分开 subplot(1,3,2);pie3(a,[0,0,0,1]);
%极坐标图 x = 1:100; theta = x/10; r = log10(x); subplot(1,4,1);polar(theta,r); theta = linspace(0,2*pi);r = cos(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);
%阶梯图和火柴梗图 x = linspace(0,4*pi,40); y = sin(x); subplot(1,2,1); stairs(y); subplot(1,2,2); stem(y);
% 填充图形 t = (1:2:15).*pi/8; x = sin(t); y = cos(t); fill(x,y,'r'); axis square off;
%数据颜色可视 [x y] = meshgrid(-3:.2:3,-3:.2:3); z = x.^2 +x.*y + y.^2; surf(x,y,z); box on; imagesc(z); colorbar; %改变颜色风格 colormap(hot); colormap(cool); colormap(gray);
%3d图(线) x = 0:0.1:3*pi; z1 = sin(x); z2 = sin(2*x); z3 = sin(3*x); y1 = zeros(size(x)); y3 = ones(size(x)); y2 = y3./2; plot3(x,y1,z1,'r',x,y2,z2,'b',x,y3,z3,'g'); grid on;
%3d图(面) x = -3.5:0.2:3.5; y = -3.5:0.2:3.5; [X Y] = meshgrid(x,y); z = X.*exp(-X.^2-Y.^2); subplot(1,2,1);mesh(X,Y,z); subplot(1,2,2);surf(X,Y,z); %投影 contour(X,Y,z) contour(z,[-.45:.05:.45])%改变紧密程度 [C H] = contour(z);clabel(C,H);%显示数值 contourf(z);%填充颜色 meshc(x,y,z);%直接在下面增加投影 surfc(x,y,z);
数值微积分
%f(x) = x^3-2x-5 p = [1 0 -2 -5]; %显示数值 x = -2:0.01:5; f = polyval(p,x); plot(x,f); %求导 polyder(p); %方程式相乘 conv(p,q); %积分 polyint(p,'常数项'); %求差 x = [1 2 5 2 1]; diff(x); %俩点斜率 x = [1 2];y= [5 7]; slope = diff(y)./diff(x); %定积分 y = @(x)1./(x.^3-2*x-5);%创建function integral(y,0,2);%上下限 %二重积分 f = @(x,y)y.*sin(x)+x.*cos(y); integral2(f,pi,2*pi,0,pi)%从里往外积分 %三重积分 f = @(x,y,z)y.*sin(x)+z.*cos(y); integral3(f,0,pi,0,1,-1,1);
方程式求根
%单一变量解 %定义符号变量 syms x; y = x*sin(x)-x; solve(y,x) %多变量解 syms x y; eq1 = x - 2*y - 5; eq2 = x + y -6; A = solve(eq1,eq2,x,y) %求解字母变量 syms x a b; solve('a*x^2-b')%x总是作为第一选择被求解 solve('a*x^2-b','b')%b为未知量 %求导 syms x; y = 4*x^5; yprime = diff(y) %积分 syms x; y = x^2*exp(x); z = int(y); z = z -subs(z,x,0);%x用0取代 求z(0) %运用function f2 = @(x)(1.2*x+0.3+x*sin(x)); fsolve(f2,0);%0为起始猜测值 fzero(f,0.1)%只能用在穿越x轴的函数 %数值解法 roots([1 -3.5 2.75 2.125 -3.875 1.25])
线性方程式与线性系统
%高斯消去法 A = [1 2 1;2 6 1;1 1 4]; b = [2;7;3]; R = rref([A b]) %LU Factorization %Ax=b; A=L^-1U; L^-1Ux=b y=Ux; %L^-1y = b;先求y Ux = y 后解x A = [1 1 1;2 3 5;4 6 8]; [L,U,P] = lu(A); %矩阵除法 A = [1 2 1;2 6 1;1 1 4]; b = [2;7;3]; x = A\b %x = A^-1*b A = [1 2 1;2 6 1;1 1 4]; b = [2;7;3]; x = inv(A)*b %检查矩阵状态 cond(A)%数值越小越健康即不为0 %线性系统 %y =Ab [v,d] = eig([2 -12;1 -5])
统计
%平均数 mean() %中位数 median() %个数最多的值 mode() %分位数 prctile() %最大/小值 max()/min() %标准差 std() %均方差 var() %盒图 marks = [80 81 81 84 92 92 94 96 97]; boxplot(marks) prctile(marks,[25 50 75]) %偏度 X = randn([10 3])*3; X(X(:,1)<0,1)=0; X(X(:,3)>0,3)=0; boxplot(X); y = skewness(X) %峰度 kurtosis() %正态总体的t检验法 load stockreturns; x1 = stocks(:,3); x2 = stocks(:,10); boxplot([x1,x2]); [h p] = ttest2(x1,x2)
曲线的回归分析与内插
%线性回归 x = [...]; y = [...]; fit = polyfit(x,y,1)%1表示x的一次方 xfit = [x(1):0.1:x(end)]; yfit = fit(1)*xfit + fit(2); plot(x,y,'ro',xfit,yfit); %散点图显示数据 scatter(x,y);box on; axis square; %相关性 corrcoef(x,y) %寻求最优拟合 for i = 1:3; subplot(1,3,i); p = polyfit(x,y,i); xfit = x(1):0.1:x(end); yfit = polyval(p,xfit); plot(x,y,'ro',xfit,yfit); end %回归分析 %regress() load carsmall; y = MPG; x1 = Weight;x2 = Horsepower; X = [ones(length(x1),1) x1 x2]; b = regress(y,X); x1fit = min(x1):100:max(x1); x2fit = min(x2):10:max(x2); [X1FIT,X2FIT] = meshgrid(x1fit,x2fit); YFIT = b(1) + b(2)*X1FIT + b(3)*X2FIT; scatter3(x1,x2,y,'filled'); hold on; mesh(X1FIT,X2FIT,YFIT); hold off; %非线性回归 cftool() %一维数据插值函数 yi = interp1(x,y,xi,'method')%'method'最临近插值;'linear'线性插值;'spline'三次样条插值;'pchip'立方插值' %二位数据插值函数 zi = interp2(x,y,z,xi,yi.'method')
相关文章推荐
- MATLAB学习笔记:矩阵基础
- 学习笔记(73):MATLAB基础入门课程-直方图hist
- 学习笔记(41):MATLAB基础入门课程-any运算
- 学习笔记(05):MATLAB零基础入门教程-工作区窗口
- [Matlab 基础] Matlab函数笔记 - 绘图
- [Matlab]基础教程学习笔记(七):符号运算
- 学习笔记(01):MATLAB零基础入门教程-MATLAB的主要特点
- Matlab学习笔记(四)——编程基础
- matlab基础笔记(二)mat文件与m文件的区别及应用
- 学习笔记(56):MATLAB基础入门课程-线型控制
- MATLAB基础视频自学笔记
- matlab学习笔记----语言基础
- 学习笔记(71):MATLAB基础入门课程-面积图area
- 学习笔记(55):MATLAB基础入门课程-polar函数
- 学习笔记(06):MATLAB零基础入门教程-编辑器窗口
- 基础知识(二)matlab与c++混合编程之经验笔记
- [Matlab]基础教程学习笔记(四):二维高层绘图操作
- 学习笔记(38):MATLAB基础入门课程-或运算
- matlab学习笔记之基础知识(一)
- [matlab]基础与应用笔记3