常规
预设->常规->初始文件夹设为上次打开的最后一个
清除命令
clc 清除command
clear 清除工作区
文件类型
脚本: 普通代码
实时脚本:分为文本和代码,可以分节运行
数学
高数
极限
lim n → ∞ ( 1 + 1 n ) n \lim_{{n \to \infty}} \left(1 + \frac{1}{n}\right)^n n→∞lim(1+n1)n
syms n;
e_approximation = limit((1 + 1/n)^n, n, inf);
fprintf('Approximation of e: %0.8f\n', e_approximation);
求导
syms x
f(x) = sin(x) + x^2;
f1 = diff(f(x));
pretty(f1);%以适合阅读的形式打印出来
积分
∫ 0 1 x 2 d x \int_{0}^{1} x^2 \, dx ∫01x2dx
f = @(x) x.^2;
integralResult = integral(f, 0, 1);
fprintf('Integral of f: %0.8f\n',integralResult);
解方程
{ x + y 2 = 13 ln ( 2 x + y ) − x y = − 2 \begin{cases}x+y^2=13\\\ln(2x+y)-x^y=-2\end{cases} {x+y2=13ln(2x+y)−xy=−2
fun = @(x)[x(1)+x(2)^2-13; log(2*x(1)+x(2))-x(1)^x(2)+2];
x0 = [4; 6];%更换初值
x = fsolve(fun, x0);
disp(x);
线代
矩阵
a= [1 3 4 5 ];
b = a+4;
plot(b)
grid on
A = [3 2 1;2 4 1;3 9 2];
B = A';%转置
[V,D] = eig(A);%特征向量 特征值
E = inv(A);%求逆
F1 = A/B%两种运算等效
F2 = A*inv(B)
F12= B\A%在斜杠下面的是求逆
%解方程Ax = b则x = A\b
G = A.*B%点对点运算,初等数学运算都记得加上
特殊矩阵
- 0矩阵
生成3行1列的零矩阵,可用于约束变量取值大于0
zeros(3,1)
画图
作图流程
准备数据,并寻找对应画图工具
定义域
x = linspace(0, 4, 1000); x= 0:0.05:30; [X,Y] = meshgrid(-2:0.1:2); axis([0 20 -1.5 1.5]);%坐标取值
标题
title('$y_1 = e^x$', 'Interpreter', 'latex')
标注
legend({ '$y_1=e^x$' '$y_2=1+x$' '$y_3=1+x+\frac{1}{2}x^2$' '$y_4=1+x+\frac{1}{2}x^2+\frac{1}{6}x^3$'},'Interpreter', 'latex','Location', 'best');
美化
grid on %开启网格
二维图
函数图
x= 0:0.05:30;% 起点:取值间隔:终点
y=sin(x);
plot(x,y,'r','LineWidth',2);
xlabel('横轴标题');
ylabel('纵轴标题');
axis([0 20 -1.5 1.5]);%坐标取值
grid on;
条形图
t = -3:0.5:3;
p = exp(-t.*t)% -t**2
bar(t,p)
barh(t,p)%horizo
极坐标
theta = 0:0.01:2*pi;
radi = abs(sin(2*theta).*cos(2*theta));
polarplot(theta,radi);
散点图
height = randn(1000,1);%(row,col),符合标准正态分布的随机
weight = randn(1000,1);
scatter(height,weight);
数据预处理
导入
主页->导入数据->excel
中文表头无法识别,自行替换
导入方式:
表: 直接导入
列矢量: 一列一列的
数值矩阵: 字符串被设为NAN
缺失
缺失太多直接删除,缺失少的用均值和众数插补,或者下面两种插值法
牛顿插值法,先构造近似函数,再取对应函数值
样条插值法,用光滑曲线去插值
异常
正态分布 : 求出均值和方差,利用3 σ \sigma σ法则排除后再用缺失的方法补上
箱型图 : 上下四分位数,取到合理区间
三维图
[X,Y] = meshgrid(-2:0.1:2);
Z = X.*exp(-X.^2-Y.^2);
surf(X,Y,Z);
colormap hsv%winter summer 主题
colorbar%色度尺
组合图
多图合一
x = linspace(0, 4, 1000);%线性间隔定义域
y1 = exp(x);
y2 = 1 + x;
y3 = 1 + x + 0.5 * x.^2;
y4 = 1 + x + 0.5 * x.^2 + 1/6 * x.^3;
figure;
hold on;
% 绘制四条曲线
plot(x, y1, 'r', 'LineWidth', 1.5); % 红色曲线
plot(x, y2, 'g', 'LineWidth', 1.5); % 绿色曲线
plot(x, y3, 'b', 'LineWidth', 1.5); % 蓝色曲线
plot(x, y4, 'm', 'LineWidth', 1.5); % 品红色曲线
title('$y_1, y_2, y_3, y_4$ Functions Comparison', 'Interpreter', 'latex');
xlabel('x', 'Interpreter', 'latex');
ylabel('y', 'Interpreter', 'latex');
legend({
'$y_1=e^x$'
'$y_2=1+x$'
'$y_3=1+x+\frac{1}{2}x^2$'
'$y_4=1+x+\frac{1}{2}x^2+\frac{1}{6}x^3$'},'Interpreter', 'latex','Location', 'best');
hold off;
多个子图
x = linspace(0, 4, 1000);
% 计算四个函数的y值
y1 = exp(x);
y2 = 1 + x;
y3 = 1 + x + 0.5 * x.^2;
y4 = 1 + x + 0.5 * x.^2 + 1/6 * x.^3;
figure
subplot(2,2,1)
plot(x, y1, 'LineWidth', 1.5)
title('$y_1 = e^x$', 'Interpreter', 'latex')
xlabel('x')
ylabel('y')
subplot(2,2,2)
plot(x, y2, 'LineWidth', 1.5)
title('$y_2 = 1 + x$', 'Interpreter', 'latex')
xlabel('x')
ylabel('y')
subplot(2,2,3)
plot(x, y3, 'LineWidth', 1.5)
title('$y_3 = 1 + x + \frac{1}{2}x^2$', 'Interpreter', 'latex')
xlabel('x')
ylabel('y')
subplot(2,2,4)
plot(x, y4, 'LineWidth', 1.5)
title('$y_4 = 1 + x + \frac{1}{2}x^2 + \frac{1}{6}x^3$', 'Interpreter', 'latex')
xlabel('x')
ylabel('y')
语法
显示
syms x
f(x) = sin(x) + x^2;
f1 = diff(f(x));
pretty(f1);
disp(f1);
fprintf
公式
格式转换
syms x
f(x) = sin(x) + x^2;
f1 = diff(f(x));
latex_f1 = latex(f1);
fprintf('%s\n', latex_f1);
2 x + cos ( x ) 2\, x + \cos\!\left(x\right) 2x+cos(x)
函数调用
语法
函数:function [ret,] = function_name(para,)
行末加分号,脚本执行就不会在命令窗口显示
findNarcissisticNumbers(3);
function findNarcissisticNumbers(n)
if nargin < 1
n = 3; % 默认为3位数
end
fprintf('%d-Digit Narcissistic Number:\n', n);
for num = 10^(n-1):10^n - 1
digits = num2str(num) - '0'; % 将字符窜数组每位减去‘0’,得拆分的数组
sum_of_powers = sum(digits.^n);
if sum_of_powers == num
fprintf('%d\n', num);
end
end
end
迭代求解
x n + 1 = 1 2 ( x n + a x n ) x_{n+1}=\frac{1} {2} ( x_{n}+\frac{\mathrm{a}} {x_{n}} ) xn+1=21(xn+xna)
number = 25;
result = sq(number);
fprintf('Square root of %d: %0.8f\n', number, result);
function result = sq(number)
x = 1;
while true
next_x = 0.5 * (x + number / x);
if abs(next_x - x) < 1e-5
result = next_x;
break;
end
x = next_x;
end
end