Function:
常见
max、min,column的最值,可以max(max(…))从而获取更精准的
sum,column的和
mean,整个column的平均
sort,每个列从小排到大(具有独立性)
sortrows,按每行首元素进行对所有行进行从小到大排序(绑定在一起)
size,矩阵大小,如输出(3 3)
find,从第一个竖着排序后寻找元素,如a=[1 2 3;4 5 6;7 8 9]中find(a==4)得到2
rem,求余数,如rem(10,5)=0
disp,打印函数(printf)
linspace(起始,终值,端点数)
switch最后是otherwise,如
input_num=1;
switch input_num
case -1
disp('nagative 1');
case 0
disp('zero');
case 1
disp('positive 1');
otherwise
disp('other value');
end
prod,乘法,用法eg.(阶乘诶)
n=1;
while prod(1:n)<1e100 %10的一百次方
n=n+1
end
for,eg
%%
for n=1:10 %中间可以加个步长n=1:1:10
a(n)=2^n;
end
disp(a)
%%字符串倒置
s1='I like the letter E';
for n=1:length(s1)
s2(n)=s1(length(s1)-n+1);
end
s2
tic-toc,计算时间
function做函数(eg.路程函数)
function x = freebody(x0,v0,t)
% calculation of free falling
% x0: initial displacement in m
% v0: initial velocity in m/sec
% t: the elapsed time in sec
% x: the depth of falling in m
x=x0+v0.*t+1/2*9.8*t.*t;
常见术语:
negative/positive 负数正数
Tips:
1.一些提示
不等于~= And&& Or||
matlab中有大于等于和小于等于号(>=,<=)
所有结构都需要end
struct.eg if-elseif-else-end
2.阵列cell eg.若想看具体内容需用[],而()类似指针指向该元素
A(1,1)={[1 4 3;0 5 8;7 2 9]};
A(1,2)={'Anne Smith'};
%或者
A{1,1}=[1 4 3;0 5 8;7 2 9];
A{1,2}='Anne Smith';
cat可以拼接--cat(1,A,B),1行2列3层
reshape可以重新塑造矩阵.若A为2*2矩阵,则reshape(A,1,4)
3.读取保存资料
若将workspace的数据保存成可阅读可编辑的文本可以用下列代码;同理可加载已保存数据
%%
clear;
%数据
save 文件名.mat -ascii
%%若使用ASCII保存,加载应使用:
load ('文件名.mat','-ascii')
若与excel结合
[Score Header]=xlsread('文件名.xlsx','范围(可不加)')
%xlswrite请自行查阅
画图
plot(),默认输入为y,x步长为1,从1开始
hold on--off可以保留图像
注释用legend
关于latex和matlab
注意,箭头坐标是归一化窗口,即0~1
#练习1
t=linspace(1,2);
y1=t.^2;
y2=sin(2*pi.*t);
hold on
plot(t,y1,'k',t,y2,'o');
xlabel('Time (ms)');
ylabel('f(t)');
title('Mini Assignment #1')
legend('t^2','sin(2\pit)','Location','northwest')
str1='$$ \int_{1}^{2} t^2 $$'
text(1.1,2.4,str1,'Interpreter','latex');
annotation('arrow','X',[0.27,0.5],'Y',[0.65,0.5]);
hold off
对坐标轴,窗口,函数line进行设置
可参考:坐标区的外观和行为 - MATLAB - MathWorks 中国
图形线条的外观和行为 - MATLAB - MathWorks 中国
(19条消息) MATLAB04:基础绘图_ncepu_Chen的博客-CSDN博客
https://blog.csdn.net/ncepu_chen/article/details/103097452
利用get和set进行读取和设置
%%对坐标轴进行设置
set(gca,'XLim',[0,2*pi]);
set(gca,'YLim',[-1.2,1.2]);
%%alternative
xlim([0,2*pi]);
ylim([-1.2,1.2]);
%%
Font字体坐标注释;Tick坐标轴精度值和标签,字符中p代表π
%% 对点
scatter(x,y,'Marker','o','LineWidth',1,...
'MarkerEdgeColor','black','MarkerFaceColor',[0.93,0.69,0.13]);
// 'Marker' 点的形状
// 'LineWidth' 点的边线粗细
// 'MarkerEdgeColor' 点的边线颜色
// 'MarkerFaceColor' 点的填充色
%% 对线
plot(x,y,'LineWidth',1.5,'Color',[0.85,0.33,0.10]);
// 'LineWidth' 线粗细
// 'Color' 线的颜色
#练习2:对练习一进行优化
t=linspace(1,2); y1=t.^2; y2=sin(2*pi.*t); hold on box on title('Mini Assignment #1') h1=plot(t,y1,'k'); h2=plot(t,y2,'-o','LineWidth',1,'MarkerEdgeColor','g','MarkerFaceColor','m','MarkerSize',5); set(gca,'Fontsize',18); set(h1,'LineWidth',3.0); xlabel('Time (ms)'); ylabel('f(t)'); legend('t^2','sin(2\pit)','Location','northwest') str1='$$ \int_{1}^{2} t^2 $$' text(1.1,2.4,str1,'Interpreter','latex'); annotation('arrow','X',[0.27,0.5],'Y',[0.65,0.5]); hold off %%初代版本对比↓ t=linspace(1,2); y1=t.^2; y2=sin(2*pi.*t); hold on plot(t,y1,'k',t,y2,'o'); xlabel('Time (ms)'); ylabel('f(t)'); title('Mini Assignment #1') legend('t^2','sin(2\pit)','Location','northwest') str1='$$ \int_{1}^{2} t^2 $$' text(1.1,2.4,str1,'Interpreter','latex'); annotation('arrow','X',[0.27,0.5],'Y',[0.65,0.5]); hold off
多个图
subplot(m,n,1);
高阶画图
logspace(-1,1,100) %生成从0.1到10一百个数字
%对数坐标轴semilogx(x,y)semilogy(x,y),loglog(x,y)
练习3.#极坐标的绘制
%% % 螺旋线 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);
GUI
%Slider
a=get(handles.slider1,'Value');b=get(handles.slider2,'Value');b=get(handles.slider2,'Value');
set(handles.text2,'String',num2str(int16(a+b)));
微分积分
1.函数句柄
2.微分积分
conv------>多项式系数矩阵相乘;
polyval(A,X)---->A为多项式系数矩阵,X为在此处的值;
polyder(A)------>derivative/dɪˈrɪvətɪv/,微分导数,(衍生物)
使用diff莫忘数据少一个若画图需要操作一手 -------->eg.plot(x(1:end-1),y)
polyint(A,K)-->K是积分出来最后的的常数
g=colormap(lines); hold on;
for i=1:3
x=0:power(10,-i):2*pi;
y=exp(-x).*sin(x.^2/2);m=diff(y)./diff(x);
plot(x(1:end-1),m,'Color',g(i,:));
end
hold off;
set(gca,'XLim',[0,2*pi]);%xlim([0 2*pi])
set(gca,'YLim',[-0.25,0.25]);
set(gca,'FontSize',18);
set(gca,'Xtick',0:pi/2:2*pi);%xticks(0:pi/2:2*pi)
set(gca,'xticklabels',{'0','\pi/2','\pi','3\pi/2','2\pi'});%xticklabels({\pi})
h=legend('h=0.1','h=0.01','h=0.001');
set(h,'fontname','Times New Roman');
box on;
定积分数值运算还可以midpoint法,trapz法,simpson‘s法
不定积分用int,定积分用integral数值积分(需@句柄)
int()可符号不定积分,int( , ,)可以定积分
computes the definite integral of
F
= int(expr,a,b)expr
froma
tob
.int
uses the default integration variable determined by symvar(expr,1
). Ifexpr
is a constant, then the default integration variable isx
.
int(expr,[a b])
is equivalent toint(expr,a,b)
.subs辅助取常数
returns a copy of
snew
= subs(s
,old
,new
)s
, replacing all occurrences ofold
withnew
, and then evaluatess
. Here,s
is an expression that contains symbolic scalar variables andold
specifies the scalar variables to be substituted.
%练习 %integral z=@(t) (t.^2 - t + 1)./(t + 3); integral(z,0,10); %int syms x; y=(x^2 - x + 1)/(x + 3); z=int(y,0,10); double(z);
关于integral
q = integral(fun,xmin,xmax)
使用全局自适应积分和默认误差容限在xmin
至xmax
间以数值形式为函数fun
求积分。
q = integral(fun,xmin,xmax,Name,Value)
指定具有一个或多个Name,Value
对组参数的其他选项。例如,指定'WayPoints'
,后跟实数或复数向量,为要使用的积分器指示特定点。eg1.
fun = @(x) exp(-x.^2).*log(x).^2; q = integral(fun,0,Inf)