%% %function的书写方式
function [a ,F]=ques1(v1,v2,t1,t2,m)
a=(v2-v1)./(t2-t1); %变量无需声明
F=m.*a; %可用iskeyword查看是否为关键字
end
%% %转置
z=[2:4;5:7;6:8];
b=a'
%disp为输出函数
a=5
if rem(a,2)==0
disp('a is a even');
else
disp('a is a odd');
end
%% %输出变量值方法
for k=100:1000
if isprime(k)==1
k %直接不加;
end
%% %whlie与for的结果形式是一样的 函数prod
n=1;
while prod(1:n)<1e100
n=n+1;
n
end
%用for求n^2和
sum=0;
for n=1:20
sum=sum+n^2;
end
%sum=sum+n典型的求和式子
sum=0;
n=1;
while n<100
sum=sum+n;
n=n+1;
end
%求n! eg:10! 1!+2!+3!+4!+5!
% 法1
function res=sum %用prod()
sum=0;
for n=1:5
i=prod(1:n);
sum=sum+i;
end
end
% 法2
mul=1; %用mul
sum=0;
for n=1:5
mul=mul*n;
sum=sum+mul;
end
%%
clear all % 清空工作区内存中的变量
close all % 关闭之前程序绘制的图像
clc % 清空之前程序在终端的输出
%% %绘图,记得x,y维度一致,只要有矩阵参与计算必有. 使维度相同
x=[1:100];
y=1./(x+1);
plot(x,y)
%% %替换 用索引下标找到再换
a=ones(5,5);
a(1,:)=a(1,:).*1
a(2,:)=a(2,:).*2
a(3,:)=a(3,:).*3
str='dgtrytrt';
str(str=='t')='a'
%% %系数方程,矩阵用/前行等于后行 用*前列等于后行
a=[1,2,3;1,4,9;1,8,27];
b=[5,-2,6]';
x=inv(a)*b
%% %记得写在命令窗口输出因变量
function [S,l]=ques1(r)
S=pi*r^2;
l=2*pi*r;
end
%% %判断质数
function res=isprime(n)
for k=2:(n-1)
if rem(n,k)==0
res=0;
else
res=1;
end
end
end
% %判断完全数=真因子之和
function res=ques1(n)
sum=0;
for i=1:(n-1)
if rem(n,i)==0
sum=sum+i;
end
end
if sum==n
n
end
end
%2000以内的完全数
for n=1:2000
sum=0;
for i=1:(n-1)
if rem(n,i)==0
sum=sum+i;
end
end
if sum==n
n
end
end
%% %求水仙花数(超完全数):某三位数各位数的立方之和
for n = 100:999 %fix舍小数点
a = fix(n/100); %floor向下取整
c = rem(n,10); %rem取余数
b = (n-a*100-c)/10;
if 100*a+10*b+c == a^3+b^3+c^3
n
end
end
%% %structure 结构体
student=struct('name','id','number','grsde');
student.name='J'; %记得用struct()创造结构体
student.id='jiuufue';
student.number=3010239;
student.grade =[100,75,73;...
95,91,85.5;100,98,72];
student(2).name='L'; %用下标可扩充或缩减
student(2).id='lojjkhn';
student(2).number=689294;
studen(2).grade =[60 65 76;...
75 78 89;90 98 100];
student(1).grade
%% %Nesting Structure %struct中还有struct用来
A=struct('date',[3 4 2 6 5],'nest',...
struct('testnum','Test 1',... %两个field一组
'xdata',[4 2 8],'ydata',[7 1 6]));%struct('field',这里是内容[值],'field'...)
A(2).date=[9 3 2;7 6 5]; %替换形成A(2)
A(2).nest.testnum='test2';
A(2).nest.xdata=[3 4 2];
A(2).nest.ydata=[5 0 9];
A.nest
%%
s1='I like the lettet';
s2=s1(length(s1):-1:1)
%% %Cell Array 注意元胞参数不要和任何内容重复
C(1,1)={ [1 5; 3 7 ;5 7] };
C(1,2)={'grtg'};
C(2,1)={1+2i};
C(2,2)={pi};
C
B{1,1}=[5 9; 3 7 ;6 2];
B{1,2}='I am too hungry';
B{2,1}=5+6i;
B{2,2}=pi;
B
%%
x=0:0.1:2*pi;y1=sin(x);y2=exp(-x);
plot(x,y1,'--*',x,y2,':o');
xlabel('t = 0 to 2\pi');
ylabel('values of sin(t) and e^{-x}');
title('Function Plots of sin(t) and e^{-x}');
legend('sin(t)','e^{-x}'); %加图例
x=0:0.5:4*pi;
y=sin(x); h=cos(x); w=1./(1+exp(-x)); g=(1/(2*pi*2)^0.5).*exp((-1.*(x-2*pi).^2)./(2*2^2));
plot(x,y,'bd-' ,x,h,'gp:',x,w,'ro-' ,x,g,'c^-'); %在一个图像内绘制多条图线
legend('sin(x)','cos(x)','Sigmoid','Gauss function'); % 添加图例
x = linspace(0,3); y = x.^2.*sin(x); plot(x,y);
line([2,2],[0,2^2*sin(2)]); %两点之间的连线
str = '$$\int_{0}^{2} x^2\sin(x) dx $$';
text(0.25,2.5,str,'Interpreter','latex'); %加注释
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);
%%插值
x=1:12;
y=[109,110,118,125,129,138,148,...
149,150,156,158,159,160];
xi=1:0.1:12;
yi=interp1(x,y,xi,'spline');
plot(x,y,'.-',xi,yi,'o');
%
x=1200:400:4000;
y=3600:-400:1200;
z= [1480 1500 1550 1510 1430 1300 1200 980;
1500 1550 1600 1550 1600 1600 1600 1550;
1500 1200 1100 1550 1600 1550 1380 1070;
1500 1200 1100 1350 1450 1200 1150 1010;
1390 1500 1500 1400 900 1100 1060 950;
1320 1450 1420 1400 1300 700 900 850;
1130 1250 1280 1230 1040 900 500 700];
xi=linspace(1200,4000,8);
yi=linspace(3600,1200,8);
zi=interp2(x,y,z,xi,yi,'cubie');
mesh(x,y,z)
%% %多项式拟合
x = [-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y = [-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
for i=1:3
p = polyfit(x,y,i); % 分别进行一次,二次,三次拟合
xfit = x(1):0.1:x(end); yfit = polyval(p,xfit);
subplot(1,3,i); plot(x,y,'ro',xfit,yfit);
legend('Data points','Fitted curve', 'Location', 'southeast');
end
%多元线性拟合
load carsmall;
y = MPG; x1 = Weight; x2 = Horsepower;% 导入数据集
X = [ones(length(x1),1) x1 x2]; % 构建增广X矩阵
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;
xlabel('Weight'); ylabel('Horsepower'); zlabel('MPG'); view(50,10);
%% %figure,plot()能让多个窗口同时出现
x=-10:0.1:10;
y1=x.^2-8;
y2=exp(x);
figure,plot(x,y1);
figure,plot(x,y2);
%% %演示axis命令的效果:
t=0:0.1:2*pi;x=3*cos(t);y=sin(t);
subplot(2,2,1);plot(x,y);axis normal
subplot(2,2,2);plot(x,y);axis square
subplot(2,2,3);plot(x,y);axis equal
subplot(2,2,4);plot(x,y);axis equal tight
x = randn(1,1000);
subplot(2,1,1);
hist(x,10);
title('Bins = 10');
subplot(2,1,2);
hist(x,50);
title('Bins = 50');
% 使用bar()和bar3()函数分别绘制二维和三维直方图
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(y); title('A 3D bargraph');
%使用barh()函数可以绘制纵向排列的柱状图
x = [1 2 5 4 8];
y = [x;1:5];
barh(y);
title('Horizontal');
%向bar()传入'stack'参数可以让柱状图以堆栈的形式画出
x=[1 2 5 4 8];
y = [x;1:5];
bar(y,'stacked');
title('Stacked');
%使用pie()和pie3()可以绘制二维和三维的饼图.向其传入
%一个bool向量表示每一部分扇区是否偏移.
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]);
%stairs()和stem()函数分别用来绘制阶梯图和针状图,
%用于表示离散数字序列.
x = linspace(0, 4*pi, 40); y = sin(x);
subplot(1,2,1); stairs(y);
subplot(1,2,2); stem(y);
%% %插值
x=[0 3 5 7 9 11 12 13 14 15];
y=[0 1.2 1.7 2.0 2.1 2.0 1.8 1.2 1.0 1.6];
xi=0:0.1:15;
yi=interp1(x,y,xi,'spline');
plot(x,y);
%% %求解
a=[9 -5 3 7];x=-2:0.01:5;
f=polyval(a,x);
plot(x,f,'LineWidth',2);
xlabel('x');ylabel('f(x)');
set(gca,'Fontsize',14)
%% %微分
a=[5 0 -2 0 1];
b=polyder(a);
polyval(b,7)
%% %求根
roots([1 -6 -12 81])
%% %积分
a=[5 0 -2 0 1];
polyint(a,3)
%% %理解for循环
hold on
for i=1:3
h=0.1^(i);
x=0:h:2*pi;y=exp(-x).*sin(x.^2./2);
plot(x(1:end-1),diff(y)./diff(x),'color',g(i,:));
end
hold off
%% %求线性方程Ax=b的解
A=rand(4,3);
b=rand(4,1);
x=A\b
x=inv(A)*b
%% %求非线性方程组的解
%二分法 求f在0.5附近的解
f=x.^3-2*x.^2+0.8;
fsolve('f',0.5)
%方程组
fun = @(x) [exp(-exp(-(x(1)+x(2))))-x(2)*(1+x(1)^2)...
,x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5];
x0 = [0,0];
x = fsolve(fun,x0) % 得到[0.3532 0.6061]
%% %多次微分
x=-2:0.005:2;y=x.^3;
m=diff(y)./diff(x);
m2=diff(m)./diff(x(1:end-1));
plot(x,y,x(1:end-1),m,x(1:end-2),m2);
xlabel('x','FontSize',18);
ylabel('y','FontSize',18);
legend('f(x)=x^3','f''(x)','f''''(x)',4);
set(gac,'FontSize',18);
function f =ques1(x)
ques1(1) = x(1)^2+x(2)^2+x(3)^2-x(1)*x(2)-x(1)*x(3);
ques1(2) = x(1)+x(2);
ques1(3) =x(2)-x(3);
end
%% %midpoint求积分
h=0.05;x=0:h:2;
midpoint=(x(1:end-1)+x(2:end))./2;
y=4*midpoint.^3;
sum(y*h)
%trapezoid求积分
h=0.05;x=0:h:2;y=4*x.^3;
s=h*trapz(y)
%% %线性规划 输出linprog()
f=[13 9 10 11 12 8];
A=[0.4 1.1 1 0 0 0;0.5 1.2 1.3 0 0 0];
b=[700;800];
Aep=[1 0 0 1 0 0;0 1 0 0 1 0;0 0 1 0 0 1];
beq=[300;500;400];
x=linprog(f,A,b,Aep,bep,[])
%% %非线性规划 输出fminbnd()
function f=ques1(x)
f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);
%定积分
y=@(x) 1./(x.^3-2*x-5);
integral(y,0,2)
%二重积分
f=@(x,y) y.*sin(x)+x.*cos(y);
integral2(f,pi,2*pi,0,pi)
%符号变量 求多元根
syms x y
eq1=x-2*y-5;
eq2=x+y-6;
A=solve(eq1,eq2,y,x)
syms b a x %顺序不重要
solve(a*x^2-b,b) %(式子,求谁的根)
syms x a b r
eq1=(x-a)^2+(y-b)^2-r^2;
solve(eq1,x)%求根
syms a b c d
[a b;c d]%symbol的矩阵
%符号微分
syms x y
diff((x^2+x*y-1)/(y^3+x+3))./diff(x)
%符号积分
sym x
y=x^2;
z=int(y,1,10);
double(z)
%原理牛顿迭代求根 Vsolve 尽量用fsolve
f2=@(x) 1.2*x+0.3+x*sin(x);
fsolve(f2,0)
sym x %??
f1=2*x(1)-x(2)-exp(-x(1));
f2=-x(1)+2*x(2)-exp(-x(2));
fsolve(f1,f2,x(1),x(2))
f=@(x) x.^2;
fzero(f,0.1) %@何时用
fsolve(f,0.1)
factorial(3)
prod(1:3)
%绘图函数
x=1:14;
re=[1 0 1 0 4 0 1 0 3 1 0 0 1 1];
subplot(1,3,1); bar(x,re); xlim([0 15]);
subplot(1,3,2); area(x,re); xlim([0 15]);
subplot(1,3,3); stem(x,re); xlim([0 15]);
%% %线性规划
f=[-2; -3 ;5];
Aeq=[1 1 1];
a=[-2 5 -1; 1 3 1];b=[-10 ;12];
beq=[7];
lb=zeros(3,1);
[x,y]=linprog(f,a,b,Aeq,beq,lb);
x,y=-y
%polyfit()求出的是拟合后的系数
x = [-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y = [-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
for i=1:3
p = polyfit(x,y,i); % 分别进行一次,二次,三次拟合
xfit = x(1):0.1:x(end); yfit = polyval(p,xfit);
subplot(1,3,i); plot(x,y,'ro',xfit,yfit);
legend('Data points','Fitted curve', 'Location', 'southeast');
end
%多元线性回归regress 也是拟合后的系数
load carsmall;
y = MPG; x1 = Weight; x2 = Horsepower; % 导入数据集?
X = [ones(length(x1),1) x1 x2]; % 构建增广X矩阵
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;
xlabel('Weight'); ylabel('Horsepower'); zlabel('MPG'); view(50,10);
% 构建样本点
xx = -2:.5:2; yy = -2:.5:3; [x,y] = meshgrid(xx,yy);
xx_i = -2:.1:2; yy_i = -2:.1:3; [x_i,y_i] = meshgrid(xx_i,yy_i);
z = x.*exp(-x.^2-y.^2);
% 线性插值
subplot(1, 2, 1);
z_i = interp2(xx,yy,z,x_i,y_i);
surf(x_i,y_i,z_i); hold on;
plot3(x,y,z+0.01,'ok','MarkerFaceColor','r'); hold on;
% 三次插值
subplot(1, 2, 2);
z_ic = interp2(xx,yy,z,x_i,y_i, 'spline');
surf(x_i,y_i,z_ic); hold on;
plot3(x,y,z+0.01,'ok','MarkerFaceColor','r'); hold on;
Matlab例题笔记
于 2023-07-13 15:08:22 首次发布