[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 and j can be used to indicate complex numbers
      • pi has the value 3.1415926…
      • ans stores the last unassigned value (like on a calculator)
      • Inf and -Inf are positive and negative infinity
      • NaN represents ‘Not a Number’
  • 若不想显示输出,则在句末加上;

  • 数组[ ]

    Two types of arrays

    1. matrix of numbers (either double or complex)
    2. cell array of objects (more advanced data structure)
    • 行向量:在[ ] 中使用逗号或空格分隔值
    • 列向量:在[ ]中使用;分隔
    • size( )显示矩阵大小
    • length( )得到矩阵长度
    • Initialize a vector of oneszeros, 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 to linspace, but generate logarithmically spaced vectors
    • Vector Indexing
      • Matlab 索引从0开始
      • The index argument can be a vector
      • 使用maxmin得到最大/小值和索引号
      • 使用find特殊值或一定范围内的索引号
      • To convert between subscripts and indices, use ind2sub, and sub2ind.

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