戳上方藍字關注我們吧
MATLAB基本語法和基本操作
1.2.1 變量賦值:
一般形式: 變量 = 表達式(數)
a = [1 2 3 ; 4 5 6 ; 7 8 9 ] %矩陣形式賦值.a = 1:2:10 %固定步長的矩陣.zeros(3,2) %三行兩列的全零矩陣.who % 檢查工作空間的變量whos % 檢查存於工作空間變量的詳細資料a = 1 2 3 4 5 6 7 8 9a = 1 3 5 7 9ans = 0 0 0 0 0 0Your variables are:A B C X Y Z a ans b f1 f2 fs i num t x y y1 y2 yy Name Size Bytes Class Attributes
1.2.2 矩陣運算
常用函數:
%* norm 範數% * det 行列式% * inv 方陣的逆矩陣% * size 矩陣的階數% * rank 秩% * trace 跡% * eig 特徵值和特徵向量% * ^ 乘方運算% * sqrtm 開方運算% * expm 指數運算% * logm 對數運算A = [6 7 5 ; 3 6 9 ; 4 1 5 ]B = 20 + AC = inv (A) * Beig(C) %求矩陣的特徵根% 矩陣的乘方運算和開方運算A = [6 7 5 ; 3 6 9 ; 4 1 5 ]B = A^2C = sqrtm(B)A = 6 7 5 3 6 9 4 1 5B = 26 27 25 23 26 29 24 21 25C = 3.8571 2.8571 2.8571 -0.9524 0.0476 -0.9524 1.9048 1.9048 2.9048ans = 4.8095 1.0000 1.0000A = 6 7 5 3 6 9 4 1 5B = 77 89 118 72 66 114 47 39 54C = 6.0000 7.0000 5.0000 3.0000 6.0000 9.0000 4.0000 1.0000 5.00001.2.3 程序控制語句
if語句循環語句if語句
x = 32 ; y = 86;if x > y 'x 大於 y' elseif x < y 'x 小於 y'elseif x == y ' x 等於y'else 'error'endans =x 小於 y循環語句
for 循環的基本格式為:for 循環變量 = 起始值 : 步長 : 終止值 循環體 end
% for循環使用示例a = 0;for i = 1:1:10 a = a + i ;endaa = 55
while循環語句基本格式為while 表達式 循環體 end
% while循環使用示例num = 0; a = 5;while a >1 a = a/2; num = num + 1;endnumnum = 31.2.4 基本繪圖方法
plot 二維線性圖subplot 繪製子圖figure() 創建一個圖的窗口titel 圖的標題xlabel x坐標ylabel y坐標grid 圖顯示網格hold 保持當前圖形clf 清除圖形和屬性mesh 三維網線圖plot3 三維圖形surf 三維表面圖繪圖的基本步驟三維圖形的繪製空間曲面的繪製繪圖的基本步驟:
x = -pi:.1:pi;y1 = sin(x);y2 = cos(x); %準備繪圖數據figure(1) %打開圖形窗口subplot(2,1,1) %確定第一幅圖繪圖窗口plot(x,y1) %以x,y1繪圖title('繪圖的基本步驟') %為第一幅圖設置標題:"繪圖的基本步驟"grid on %顯示網格線subplot(2,1,2) %確定第二幅圖繪圖窗口plot(x,y2) %以x,y2繪圖xlabel('time') %為第二幅設置x坐標名'time'ylabel('y') %為第二幅設置y坐標名'y'figure(2) %打開圖形窗口subplot(1,2,1),stem(x,y1,'r') %繪製紅色的脈衝圖subplot(1,2,2),errorbar(x,y1,'g') %繪製綠色的誤差條形圖
三維圖形的繪製
figure(3)x = 0:0.1:4*pi;y1 = sin(x);y2 = cos(x);plot3(y1,y2,x)title('繪圖的三維圖形')grid on
空間曲面的繪製
x = [-2:0.2:2];y = x;[X,Y] = meshgrid(x,y);Z = X.*exp(-X.^2-Y.^2);subplot(2,2,1) % 繪製子圖第一幅surf(Z);shading flatsubplot(2,2,2) % 繪製子圖第二幅mesh(Z);subplot(2,2,3) % 繪製子圖第三幅meshc(Z)subplot(2,2,4) % 繪製子圖第四幅surfl(Z)view(20,7)
掃描這裡關注我們