Matlab - 基础使用

基础运算

a = 1/2; //0.5
b = 1-2; //-1
c = 2*pi; //6.2832
d = 2^3;//8

常见数学函数

  • exp: exponentiation (e^)
  • abs: absolute value
  • cos, sin, tan :cosine, sine and tangent
  • acos,asin,atan :inverse of cosine, sine and tangent
  • cosh,sinh,tanh: hyperbolic functions (also inverses)
  • sqrt :square root
  • conj :complex conjugate
  • fix round :toward zero
  • round :round toward nearest integer
  • log :natural logarithm
  • log10 :base 10 logarithm
  • real :real part of complex number
  • imag :imaginary part of complex number

矩阵表示

a=[1 2 3;4 5 6];
//1 2 3
//4 5 6
b=[1:1:3] //b = 1 2 3
c=[0:2:10] //c = 0 2 4 6 8 10
d=linspace(-3,3,100); // 返回100个-3~3之间的数
[x,y] = meshgrid(x,y); 
a = a.*3; //a中每个数值都 *3

A(2,3) 第二行第三列
A(2,:) 第二行
A(:,3) 第三列
A(1:3,3:4) 1~3行,3~4列

数据传入,保存

可以使用 importdata(‘a.txt’)
或者load上传数据
使用 save 将workplace数据保存到mat文件。

>> A=importdata('Temp.txt')
A =
    data: [4x4 double]
    textdata: {'R1' 'R2' 'R3' 'R4'}
    colheaders: {'R1' 'R2' 'R3' 'R4'}
>> A.data
ans =
    88.4000 91.5000 89.2000 77.3000
    83.2000 88.0000 67.8000 91.0000
    77.8000 76.3000 90.3000 92.5000
    92.1000 96.4000 81.2000 84.6000
>> A.textdata
ans =
    'R1' 'R2' 'R3' 'R4'

条件语句 if else,switch

if condition1
    statements executed if
    condition1 is true
elseif condition2
    statements executed if
    condition2 is true
else
    statements executed if
    neither condition1 or
    condition2 is true
end
switch switch_expression
case case_expression
    statements
case case_expression
    statements
:
otherwise
    statements
end

循环语句 for, while

for count=0:2:20
    statement;
end
while (isempty(userString) ||userString=='Y' || userString=='y')
    userString = input('Do you want to repeat the loop? y/n :','s');
    if (isempty(userString))
        userString='Y';
    end;
end

函数

自定义函数

function [c] = add(a,b)
    c = a+b;
end

图像

Matlab - 基础使用_第1张图片

>> t=[0:0.01:10];
>> p1=cos(t);
>> p2=sin(t);
>> p3=p1.*p2;
>> plot(t,p1,t,p2,t,p3)
>> title('Example Plot')
>> xlabel('t (seconds)')
>> legend('cos', 'sin','cos(t)*sin(t)')

Matlab - 基础使用_第2张图片

你可能感兴趣的:(Matlab - 基础使用)