[MITOPENCOURSEWARE] 6.094 Introduction to Programming in MATLAB
来源:互联网 发布:随身wifi网络不稳定 编辑:程序博客网 时间:2024/05/20 23:02
Introduction to MATLAB
Lecture 1 Variables, Scripts, and Operations
Matlab Basic
- MATLAB can be thought of as a super-powerful graphing calculator
- In addition it is a programming language
一些命令
帮助命令
- help [ ]
- doc [ ] 更详细的帮助
注释
%
第一行注释为脚本的帮助文档
新建脚本
edit
edit [ ].m
输出字符串
disp
注意字符串使用单引号
保存变量到文件中
save
save myFile a b
- saves variables a and b to the file myfile.mat
- myfile.mat file is saved in the current directory
移除当前环境中变量
clear
加载文件中变量
load
清理命令窗口
clc
变量
- 创建和修改的变量会一直保存在工作区
- Matlab is a weekly typed language. 不需要初始化变量
- 变量命名:首位必须是字母;以字母、数字、下划线_ 组成
- 大小写敏感
- Built-in variables. Don’t use these names!
i
andj
can be used to indicate complex numberspi
has the value 3.1415926…ans
stores the last unassigned value (like on a calculator)Inf
and-Inf
are positive and negative infinityNaN
represents ‘Not a Number’
若不想显示输出,则在句末加上
;
数组[ ]
Two types of arrays
- matrix of numbers (either double or complex)
- cell array of objects (more advanced data structure)
- 行向量:在[ ] 中使用逗号或空格分隔值
- 列向量:在[ ]中使用
;
分隔 size( )
显示矩阵大小length( )
得到矩阵长度
- Initialize a vector of
ones
,zeros
, or random numbers
»o=ones(1,10)
row vector with 10 elements, all 1
»z=zeros(23,1)
column vector with 23 elements, all 0
»r=rand(1,45)
row vector with 45 elements (uniform [0,1])
»n=nan(1,69)
row vector of NaNs (useful for representing uninitialized variables) - To initialize a linear vector of values use
linspace
»a=linspace(0,10,5)
starts at 0, ends at 10 (inclusive), 5 values
Can also use colon operator (:
)
»b=0:2:10
starts at 0, increments by 2, and ends at or before 10
increment can be decimal or negative
»c=1:5
if increment isn’t specified, default is 1 - To initialize logarithmically spaced values use
logspace
similar tolinspace
, but generate logarithmically spaced vectors - Vector Indexing
- Matlab 索引从0开始
- The index argument can be a vector
- 使用
max
和min
得到最大/小值和索引号 - 使用
find
特殊值或一定范围内的索引号 - To convert between subscripts and indices, use
ind2sub
, andsub2ind.
Homework 1
- 科学技术法使用
e
表示 - 虚数可以直接使用虚数单位i,j 表示也可以使用复数函数
complex( )
- 以e为底数,使用
exp( )
- 产生从a到b之间的随机数
r=a+(b-a)*rand(m,n)
- 数除矩阵要在除号前面加点
.
,平方也是 - 取平均值使用
mean
,可选择行或列
// 不保证没错
a = 10;
b = 2.5e23;
c = 2+3i;
d = exp((j*2*pi)/3);
aVec = [3.14 15 9 26];
bVec = [2.71;8;28;182];
cVec = 5:-0.2:-5;
dVec = logspace(0,1,100);
eVec = ['Hello'];
aMat = ones(9) * 2;
bMat = diag([1 2 3 4 5 4 3 2 1]);
cMat = reshape(1:100,[10 10]);
dMat = NaN(3,4);
eMat = [13 -1 5;-22 10 -87];
fMat = floor(rand(5,3)*6-3);
x = 1 / (1 + exp(-(a - 15)/6));
y = (sqrt(a) + b^(1/21))^pi;
z = log(real((c + d) * (c - d)) * sin(a * pi / 3))/(c * conj(c));
xVec = (1 / (sqrt(2 * pi * 2.5^2))) * exp(-power(cVec,2) / (2 * 2.5 * 2.5));
yVec = sqrt(power((aVec)',2) + power(bVec,2));
zVec = log10(1 ./ dVec);
xMat = (aVec * bVec) * aMat.^2;
yMat = bVec * aVec;
zMat = det(cMat) * (aMat * bMat)';
cSum = sum(cMat);
eMean = mean(eMat,2);
eMat = [1 1 1;eMat(2,:)];
cSub = cMat(2:9,2:9);
lin = 1:20;lin(2:2:20) = -lin(2:2:20);
r = rand(1,5);r(find(r<0.5)) = 0;
- 构造正余弦曲线使用函数
linspace
t = linspace(0,2*pi,100);
plot(t,sin(t));
hold on;
plot(t,cos(t),'r--');
xlabel('Time(x)');
ylabel('Function value');
title('Sin and Cos functions');
legend('Sin','Cos');
xlim([0 2*pi]);
ylim([-1.4 1.4]);
0 0
- [MITOPENCOURSEWARE] 6.094 Introduction to Programming in MATLAB
- An Introduction to Programming and Numerical Methods in MATLAB
- 课程: Introduction to Programming with MATLAB in coursera
- Introduction to Asynchronous Programming in .NET 2.0
- An Introduction to Interactive Programming in Python
- Introduction to C++ Programming in UE4
- Introduction to C++ Programming in UE4
- Introduction to Functional Programming in Swift
- Introduction to MMX Programming
- Introduction to Java Programming
- Introduction to Windows Programming
- Introduction to SSE Programming
- Introduction to RDMA programming
- Introduction to Programming in C/C++ with Vim
- Introduction to Programming in C/C++ with Vim
- Introduction to Programming in C/C with Vim
- An Introduction to Socket Programming in .NET(转载)
- An introduction to Z-Wave programming in C#
- Hadoop之运行wordcount
- 用IAR开发第一个程序,点亮led灯以及建立工程的步骤。
- SHELL编程
- Shell脚本编程30分钟入门
- C++中智能指针的工作原理和简单实现
- [MITOPENCOURSEWARE] 6.094 Introduction to Programming in MATLAB
- 网站会员整合ucenter 实现多站点同步登录方法
- input属性placeholder
- 谁来拯救我的猿猿君?
- 将html网页改成jsp
- 介绍一个开源的gifview——Android显示GIF动画
- android 应用退到后台,类似最小化
- a的信息
- 2015-01-19 废话连篇