matlab實驗報告

實驗一 MATLAB的基本使用方法

一、實驗目的和要求:

通過完成實驗一,掌握MATLAB的基本使用方法。

二、實驗內容:

1.練習數據和符號輸入法,將前面的命令在命令窗口中執行通過。

1)>>5
2)>>x=[1  2   3   4]
3)>>g=[1   2   3   4];h=[4   3   2   1]
>>s1=g+h,  s2=g.*h,   s3=g.^h
   S1=
       5   5   5   5
   S2=
       4   6   6   4
   S3=
       1   8   9   4

這裏寫圖片描述

2.輸入A=[715;256;315],B=[111;222;333],在命令窗口中執行下列表達式,掌握其含義。

 源程序:>>A=[7 1 5;2 5 6;3 1 5];
          B=[1 1 1;2 2 2;3 3 3];
          >>A(2,3)            
          >>A(:,2)
          >>A(:,1:2:3)       % A數組中第一列和第三列            
           >>A(3,:)          %A數組中第三行             
           >>A(:,3).*B(:,2)    %A中第三列和B中第二列相乘
           >> A(:,3)*B(2,:)   %A中第三列和B中第二行相乘豎着排,形成3*3矩陣               
           >>A*B            %A 與B數組相乘            
           >>A.*B
        >>A^2           
           >>A.^2          % A中所有數平方          
           >>B/A            %B矩陣除以A矩陣          
           >>B./A           % B中數除以A中對應位置數

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

3.輸入C=.1:2:20,則C(i)表示什麼?其中i=1,2,3…10;

源程序:>>c=1:2:20;
        >>c(1)
        >>c(10)

這裏寫圖片描述

實驗二 MATLAB的數值計算

一、實驗目的和要求:

通過完成實驗二,掌握MATLAB的數值計算。

二、實驗內容:

1.用二分法計算多項式方程X^3-2X-5=0在[0,3]內的一個根。

a=0;fa=-inf;
        b=3;fb=inf;
        while  b-a>eps*b
              x=(a+b)/2;
              fx=x^3-2*x-5;
            if sign(fx)==sign(fa)
              a=x;fa=fx
            else
              b=x;fb=fx
            end
        end
        x
運行結果:
          x=2.0945515148154233

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

2.矩形的創建,加減運算

A=[1,1,1;1,2,3;1,3,6]
B=[8,1,6;3,5,7;4,9,2]
A+B=A+B
A-B=A-B
在Matlab命令窗口建入,則
    結果顯示    A+B=
                     9   2    7
                     4   7   10
                     5   12   8
                A-B=
                     -7   0   -5
                     -2   -3   -4 
                     -3   -6   4

這裏寫圖片描述

3.混合積

 eg. 計算向量a=(1,2,3),b=(4,5,6)和c=(-3,6,-3)的混合積a.(b*c)
           在MATLAB編輯器中建立M文件
           a=[1   2   3];b=[4   5   6];c=[-3   6   -3]
           x=dot(a,cross(b,c))

這裏寫圖片描述

這裏寫圖片描述

4.符號矩陣的四則運算

  eg.   A=sym(‘[1/x,1/(x+1); 1/(x+2), 1/(x+3)]’)
          B=sym(‘[x,1;x+2,0]’);
         C=B-A
         D=A\B
     則顯示
         C=
         x-1/x      1-1/(x+1)
         x+2-1/(x+2)     -1/(x+3)
         D=
         -6*x-2*x^3-7*x^2         1/2*x^3+x+3/2*x^2
         6+2*x^3+10*x^2+14*x    -2*x^2-3/2*x-1/2*x^3

這裏寫圖片描述

5.向量組的最大無關組


    Eg.
      a1=(1,-2,2,3),a2=(-2,4,-1,3),a3=(-1,2,0,-3),a4=(0,6,2,3),a5=(2,6,3,4)的一個最大無關組         在MATLAB編輯器中建立M文件
a1=[1   -2   2   3]’;
a2=[-2   4   -1  3]’;
a3=[-1   2   0   3]’;
a4=[0   6   2    3]’;
a5=[2   -6   3   4]’;
A=[a1   a2   a3   a4   a5]
format rat    %以有理數格式輸出
B=rref(A)    %求A的行最簡形
運行後的結果爲
A=
1   -2   -1   0   2
-2   4   2    6   -6
-2   -1   0   2   3
3    3    3   3   4
B=
1   0   1/3   0   16/9
0   1   2/3   0   -1/9
0   0    0    1   -1/3
0   0    0    0    0 

從中可以得到;向量a1 a2 a4爲其中最大無關組

這裏寫圖片描述

實驗三 MATLAB的符號計算

一、實驗目的和要求:

通過完成實驗三,掌握MATLAB的符號計算。

二、實驗內容:

1.使用sym函數定義符號變量和符號表達式

使用sym函數定義符號表達式 ax^2+bx+c
     >>a=sym(‘a’)
     >>b= sym(‘b’)
     >>c= sym(‘c’)
     >>x=sym(‘x’)
     >>f=a*x^2+b*x+c
       f=
          a*x^2+b*x+c

這裏寫圖片描述

2.findsym函數用於尋找符號變量

>>syms  a  alpha   b   x1 y
>>findsym  (alpha+a+b)
  ans=
      a,alpha,b
>>findsym(cos(a’pha)*b*x1+14*y,2)
  ans=
      x’,y
>>findsym(y*(4+3*i)+b*j)
  ans=
      y
>>

這裏寫圖片描述

3.符號表達式的四則運算

例: >>syms x  y  a  b
     >>fun1=sin(x)+cos(y)
       fun1=
            sin(x)+cos(y)
     >>fun=a+b
       fun=
           a+b
   >>fun1+fun2
     ans=sin(x)+cos(y)+a+b
   >>fun1*fun2
      ans=
           (sin(x)+cos(y)*(a+b)

這裏寫圖片描述

4.由數值矩陣轉換爲符號矩陣

   例:>>u=[19  1   2   4;6   1   7   8;9   2   16   3;3   4   3   20]
         u=
            19   1   2   4
             6   1   7   8
             9   2   16  3
             3   43   2  0
      >>s=sym(u)
        s=
           [  19  ,  1   ,   2   ,   4]
           [  6   ,  1   ,   7   ,   8]
           [  9   ,  2   ,   16  ,   3]
           [  3   ,  43  ,   2   ,   0]  

這裏寫圖片描述

5.jacobian函數的使用

   例: >>sym s  x  y  z
        >>a=[x^2+x*y;sin(x)*cos(y)]
          a=
            [x^2+x*y]
            [sin(x)+cos(y)]
        >>jacobian (a,[x,y])          
              [2*x+y,x]
              [cos(x)*cos(y),-sin(x)*sin(y)]

這裏寫圖片描述

實驗四 MATLAB的程序設計

一、實驗目的和要求:

通過完成實驗四,掌握MATLAB的程序設計。

二、實驗內容:

1.函數

例:function  isleapyear(year)
sign=0;
if  rem(year,4)==0
     sign=sign+1;
  end
if rem(year,100)==0
      sign=sign-1;
end
if rem(year,400)==0
      sign=sign+1;
end
if sign>=1
    fprintf(‘%4d year is a leap year.\n’,year)
  else 
     fprintf(‘%4d year is not a leap year.\n’,year)
end
>>y1=1000;
>>y2=2000;
>>y3=1996;
>>y4=3000;
>>isleapyear(y1)
1000 year is not a leap year
>>isleapyear(y2)
2000 year is  a leap year
>>isleapyear(y3)
1996 year is a leap year
>>isleapyear(y4)
3000 year is not a leap year

這裏寫圖片描述

這裏寫圖片描述

2.將百分數的分數轉化爲五級制分數 90~100:優 80~90:良70~79:中 60~69:及格 59以下爲不及格

>>function f=change(score)
a=floor (score/10)
switch a
case {10,9}
f=’該成績爲優’
case 8
f=’該成績爲良’
case 7
f=’該成績爲中’
case 6
f=’該成績爲及格’
otherwise
f=’該成績爲不及格’
end

運行結果:
>>b=88
b=
88
>>change(b)
a=
8
f=
該成績爲良

這裏寫圖片描述

這裏寫圖片描述

3.編寫三個子函數和一個主函數,實現下列函數在點x=0.5,2.5,6.5處的計算

>>function r=zhs(x)
            if   x<=2/3
                 r=zihanshu1(x)
            else if 2/3<x&x<=6
                 r=zihanshu2(x) 
            else  r=zihanshu3(x)
            end
         function r1=zihanshu1(x)
                 r1=2*x-3
         function r2=zihanshu2(x)
                 r2=(sin(x)+cos(x)^2)/(1+x^2)
         function r3=zihanshu3(x)
                 r3=(x^3+52/(tan(x)+6)
        運行結果:
                 zhs(0.5)
                  r1=-2
                  r=-2
                 ans=
                      -2
                  zhs(2.5)
                  r2=0.1711
                  r=0.1711
                  ans=
                      0.1711
                  zhs(6.5)
                  r3=44.9538
                  r=44.9538
                  ans=
                       44.9538 

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

實驗五 MATLAB 計算結果的可視化

一、實驗目的和要求:

通過完成實驗一,掌握MATLAB的基本繪圖方法。

二、實驗內容:

1.繪製二維進線圖

例:>>x=0:0.11:10;
>>y1=sin(x);
>>y2=cos(x-2.5);
>>y3=tan(x+1.5)
>>plot(x,y1,x,y2,x,y3)

這裏寫圖片描述

2.文本標註和交互式文本標註

例:>>x=linspace(-3,5,100);
   >>y=cos(x);
   >>z=sin(x);
   >>plot(x,y,x,z);
   >>title('一條正弦曲線和一條餘弦曲線’)
   >>xlabel('x的取值範圍’)
   >>ylabel('y和z的值’)

這裏寫圖片描述

這裏寫圖片描述

3.條形圖和麪積圖

例 >>y=[1  2  3  4  5  6  7  8  9  10]
   >>y
     y=
1  2  3  4  5  6  7  8  9  10
>>bar(y) 

這裏寫圖片描述

4.給圖形添加標題

>> x=0:.1:2;
>> y1=sin(x);
>> y2=sin(x-0.25);
>> y3=sin(x-0.5);
>> plot(x,y1,'-.b',x,y2,'--r*',x,y3,'-.gh')

這裏寫圖片描述

這裏寫圖片描述

5.view函數的使用

>>[x,y]=meshgrid([-5:0.2:5]);
>>z=exp(0.5*(x.^2-y.^2));
>>surf(x,y,z)

>>view(3060

這裏寫圖片描述

這裏寫圖片描述

6.編寫三個子函數和一個主函數,實現下列函數在點x=0.5, 2.5, 6.5處的計算

      解:
                   function  r=mf(x)
                   if   x<=2/3
                          r=zihanshu1(x)
                   elseif   2/3<x&x<=6
                           r=zihanshu2(x)
                   else
                         r=zihanshu3(x)
                   end

                   function  r1=zihanshu1(x)
                   r1=2*x-3
                   function  r2=zihanshu2(x)
                     r2=(sin(x)+cos(x)^2)/(1+x^2)

                   function  r3=zihanshu3(x)
                     r3=(x^3+5)/(tan(x)+6)
               >>mf(0.5)
                     ans=
-2
               >>mf(2.5)
                     ans=
                           0.1711
               >>mf(6.5)
                     ans=
44.9538

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章