穨matlab教學範例ccc.doc

Similar documents
ENGG1410-F Tutorial 6

Untitled-3

Microsoft PowerPoint - Lecture7II.ppt

Computer Architecture

Preface This guide is intended to standardize the use of the WeChat brand and ensure the brand's integrity and consistency. The guide applies to all d

untitled

Microsoft PowerPoint - CH 04 Techniques of Circuit Analysis

一 課 後 社 團 名 稱 :B02. 直 排 輪 校 隊 C 班 ( 校 隊 班 ) 二 授 課 教 師 : 劉 輔 人 助 教 : 杜 翊 嘉 世 界 盃 滑 輪 溜 冰 錦 標 賽 世 界 冠 軍 榮 獲 VOUGE 時 尚 雜 誌 專 訪 同 週 一 校 隊 班 介 紹

科学计算的语言-FORTRAN95

余德浩诗词

Guide to Install SATA Hard Disks

Captive Screws Styled knob series M3 thread size Smooth knob meets UL-1950 Designed for hand operation Spring ejected Wide variety of sizes, re

2015年4月11日雅思阅读预测机经(新东方版)

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Microsoft PowerPoint - ch6 [相容模式]

PowerPoint Presentation

coverage2.ppt

区 域 活 动 进 入 中 班 我 们 区 域 的 设 置 和 活 动 材 料 都 有 所 变 化, 同 时 也 吸 引 孩 子 们 积 极 的 参 与 学 习 操 作 区 的 新 材 料 他 们 最 喜 欢, 孩 子 们 用 立 方 块 进 行 推 理 操 作 用 扑 克 牌 进 行 接 龙 游

epub83-1

MATLAB 1

Epson

穨control.PDF

Microsoft Word - 3D手册2.doc

Introduction to Hamilton-Jacobi Equations and Periodic Homogenization

VASP应用运行优化

TX-NR3030_BAS_Cs_ indd

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

lnag_ch_v2.01.doc

三維空間之機械手臂虛擬實境模擬

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

PowerPoint Presentation

衛星影像分類



C/C++ - 函数

BC04 Module_antenna__ doc

Windows XP

Lecture #4: Several notes 1. Recommend this book, see Chap and 3 for the basics about Matlab. [1] S. C. Chapra, Applied Numerical Methods with MATLAB

Gerotor Motors Series Dimensions A,B C T L L G1/2 M G1/ A 4 C H4 E

Improved Preimage Attacks on AES-like Hash Functions: Applications to Whirlpool and Grøstl

The Development of Color Constancy and Calibration System

Microsoft PowerPoint _代工實例-1

國立桃園高中96學年度新生始業輔導新生手冊目錄

東吳大學 104 學年度碩士班研究生招生考試試題第 2 頁, 共 7 頁 5. Consider a project with the following cash flows. Year Cash Flow 0 -$16, , ,000 What s the IRR o

從詩歌的鑒賞談生命價值的建構

untitled

Microsoft Word - Final Exam Review Packet.docx

States and capital package

MODEL COLOR LIST UZ125D2 YMW GRAY YNF RED YRG BLUE 30H WHITE

Index of Zhengtong Daozang

bbc_bond_is_back_worksheet.doc

Chapter 24 DC Battery Sizing

Microsoft Word - ch05note_1210.doc

PCPDbooklet_high-res.pdf

C/C++程序设计 - 字符串与格式化输入/输出

WTO

Serial ATA ( Silicon Image SiI3114)...2 (1) SATA... 2 (2) B I O S S A T A... 3 (3) RAID BIOS RAID... 5 (4) S A T A... 8 (5) S A T A... 10

C++ 程式設計

UDC Empirical Researches on Pricing of Corporate Bonds with Macro Factors 厦门大学博硕士论文摘要库

HC20131_2010

HC50246_2009

國立中山大學學位論文典藏.PDF

< D313738B1F5A46CB5C4B773B1B42DB4BFA5C3B8712E706466>

( )

Bus Hound 5

東莞工商總會劉百樂中學

Microsoft PowerPoint - NCBA_Cattlemens_College_Darrh_B

untitled

一年級上學期(2001/2002)測驗時間表及範圍 /10/2001

(baking powder) 1 ( ) ( ) 1 10g g (two level design, D-optimal) 32 1/2 fraction Two Level Fractional Factorial Design D-Optimal D

Microsoft Word 記錄附件

System Design and Setup of a Robot to Pass over Steps Abstract In the research, one special type of robots that can pass over steps is designed and se

ebook73-29

Microsoft Word - ChineseSATII .doc

不 知 肉 味 的 用 法 相 同? (A) 長 煙 一 空, 皓 月 千 里 (B) 五 臟 六 腑 裡, 像 熨 斗 熨 過, 無 一 處 不 伏 貼 (C) 兩 片 頑 鐵, 到 他 手 裡, 便 有 了 五 音 十 二 律 似 的 (D) 吾 觀 三 代 以 下, 世 衰 道 微 12. 文

ch_code_infoaccess

Microsoft Word 軟體設計第二部份範例試題_C++_ _1_.doc

larson_appl_calc_ch01.qxd.ge

C/C++语言 - 运算符、表达式和语句

Microsoft Word - p11.doc

2017 CCAFL Chinese in Context

Text 文字输入功能 , 使用者可自行定义文字 高度, 旋转角度 , 行距 , 字间距离 和 倾斜角度。

Microsoft PowerPoint - STU_EC_Ch08.ppt

Transcription:

MATLAB ( Math Dept. of University of New Hampshire) ( ) 1. Tutorial on vectors 2. Tutorial on matrices 3. Tutorial on vector operations 4. Tutorial on loops 5. Tutorial on plots 6. Tutorial on executable files 7. Tutorial on subroutines 8. Tutorial on if statements 90 10 05 1

>> 0:2:8 0 2 4 6 8 >> ans' 8 0 2 4 6 >> v = [0:2:8] v = >> v v = >> v; >> v' 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 >> v(1:3) 2

0 2 4 >> v(1:2:4) 0 4 >> v(1:2:4)' 0 4 >> v(1:3)-v(2:4) -2-2 -2 >> A = [ 1 2 3; 3 4 5; 6 7 8] A = 1 2 3 3 4 5 6 7 8 >> B = [ [1 2 3]' [2 4 7]' [3 5 8]'] B = 1 2 3 2 4 5 3 7 8 >> whos Name Size Elements Bytes Density Complex A 3 by 3 9 72 Full No B 3 by 3 9 72 Full No ans 1 by 3 3 24 Full No v 1 by 5 5 40 Full No 3

Grand total is 26 elements using 208 bytes >> v = [0:2:8] v = >> A*v(1:3) 0 2 4 6 8??? Error using ==> * Inner matrix dimensions must agree. >> A*v(1:3)' 16 28 46 >> A(1:2,3:4)??? Index exceeds matrix dimensions. >> A(1:2,2:3) 2 3 4 5 >> A(1:2,2:3)' 2 4 3 5 >> inv(a) Warning: Matrix is close to singular or badly scaled. 1.0e+15 * Results may be inaccurate. RCOND = 4.565062e-18-2.7022 4.5036-1.8014 5.4043-9.0072 3.6029-2.7022 4.5036-1.8014 4

>> inv(a)??? Undefined function or variable a. >> eig(a) 14.0664-1.0664 0.0000 >> [v,e] = eig(a) v = e = -0.2656 0.7444-0.4082-0.4912 0.1907 0.8165-0.8295-0.6399-0.4082 14.0664 0 0 >> diag(e) 14.0664-1.0664 0.0000 0-1.0664 0 0 0 0.0000 >> v = [1 3 5]' v = 1 3 5

5 >> x = A\v Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 4.565062e-18 x = 1.0e+15 * 1.8014-3.6029 1.8014 >> x = B\v x = 2 1-1 >> B*x 1 3 5 >> x1 = v'/b x1 = 4.0000-3.0000 1.0000 >> x1*b 1.0000 3.0000 5.0000 >> clear >> whos >> v = [1 2 3]' v = 1 6

2 3 >> b = [2 4 6]' b = 2 4 6 >> v+b >> v-b 3 6 9-1 -2-3 >> v*b Error using ==> * Inner matrix dimensions must agree. >> v*b' >> v'*b 2 4 6 4 8 12 6 12 18 28 >> v.*b 2 7

8 18 >> v./b 0.5000 0.5000 0.5000 >> sin(v) 0.8415 0.9093 0.1411 >> log(v) 0 0.6931 1.0986 >> x = [0:0.1:100] x = Columns 1 through 7 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 (stuff deleted) Columns 995 through 1001 99.4000 99.5000 99.6000 99.7000 99.8000 99.9000 100.0000 >> y = sin(x).*x./(1+cos(x)); >> plot(x,y) >> plot(x,y,'rx') >> help plot 8

PLOT Plot vectors or matrices. PLOT(X,Y) plots vector X versus vector Y. If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up. PLOT(Y) plots the columns of Y versus their index. If Y is complex, PLOT(Y) is equivalent to PLOT(real(Y),imag(Y)). In all other uses of PLOT, the imaginary part is ignored. Various line types, plot symbols and colors may be obtained with PLOT(X,Y,S) where S is a 1, 2 or 3 character string made from the following characters: y yellow. point m magenta o circle c cyan x x-mark r red + plus g green - solid b blue * star w white : dotted k black -. dashdot -- dashed For example, PLOT(X,Y,'c+') plots a cyan plus at each data point. PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by the (X,Y,S) triples, where the X's and Y's are vectors or matrices and the S's are strings. For example, PLOT(X,Y,'y-',X,Y,'go') plots the data twice, with a solid yellow line interpolating green circles at the data points. The PLOT command, if no color is specified, makes automatic use of the colors specified by the axes ColorOrder property. The default ColorOrder is listed in the table above for color systems where the default is yellow for one line, and for multiple lines, to cycle through the first six colors in the table. For monochrome systems, PLOT cycles over the axes LineStyleOrder property. PLOT returns a column vector of handles to LINE objects, one handle per line. The X,Y pairs, or X,Y,S triples, can be followed by 9

parameter/value pairs to specify additional properties of the lines. See also SEMILOGX, SEMILOGY, LOGLOG, GRID, CLF, CLC, TITLE, XLABEL, YLABEL, AXIS, AXES, HOLD, and SUBPLOT. >> plot(x,y,'y',x,y,'go') >> plot(x,y,'y',x,y,'go',x,exp(x+1),'m--') >> whos Name Size Elements Bytes Density Complex ans 3 by 1 3 24 Full No b 3 by 1 3 24 Full No v 3 by 1 3 24 Full No x 1 by 1001 1001 8008 Full No y 1 by 1001 1001 8008 Full No Grand total is 2011 elements using 16088 bytes >> coef = zeros(1,1001); >> coef(1) = y(1); >> y = (y(2:1001)-y(1:1000))./(x(2:1001)-x(1:1000)); >> whos Name Size Elements Bytes Density Complex ans 3 by 1 3 24 Full No b 3 by 1 3 24 Full No coef 1 by 1001 1001 8008 Full No v 3 by 1 3 24 Full No x 1 by 1001 1001 8008 Full No y 1 by 1000 1000 8000 Full No Grand total is 3011 elements using 24088 bytes >> coef(2) = y(1); >> y(1) 0.0500 >> y = (y(2:1000)-y(1:999))./(x(3:1001)-x(1:999)); >> coef(3) = y(1); 10

>> for j=1:4, j j = 1 j = 2 j = 3 j = >> 4 >> v = [1:3:10] v = 1 4 7 10 >> for j=1:4, v(j) = j; 11

>> v v = 1 2 3 4 >> A = [ [1 2 3]' [3 2 1]' [2 1 3]'] A = >> B = A; 1 3 2 2 2 1 3 1 3 >> for j=2:3, A = A = A(j,:) = A(j,:) - A(j-1,:) 1 3 2 1-1 -1 3 1 3 1 3 2 1-1 -1 2 2 4 >> for j=2:3, B = B = for i=j:3, B(i,:) = B(i,:) - B(j-1,:)*B(i,j-1)/B(j-1,j-1) 1 3 2 0-4 -3 3 1 3 1 3 2 12

0-4 -3 0-8 -3 B = 1 3 2 0-4 -3 0 0 3 >> h = 0.1; >> x = [0:h:2]; >> y = 0*x; >> y(1) = 1; >> size(x) 1 21 >> for i=2:21, y(i) = y(i-1) + h*(x(i-1)^2 - y(i-1)^2); >> plot(x,y) >> plot(x,y,'go') >> plot(x,y,'go',x,y) >> h = 0.001; >> x = [0:h:2]; >> y = 0*x; >> y(1) = 1; >> i = 1; >> size(x) 1 2001 >> max(size(x)) 2001 >> while(i<max(size(x))) y(i+1) = y(i) + h*(x(i)-abs(y(i))); 13

i = i + 1; >> plot(x,y,'go') >> plot(x,y) >> h = 1/16; >> x = 0:h:1; >> y = 0*x; >> size(y) 1 17 >> max(size(y)) 17 >> y(1) = 1; >> for i=2:max(size(y)), y(i) = y(i-1) + h/y(i-1); >> true = sqrt(2*x+1); >> plot(x,y,'go',x,true) >> plot(x,abs(true-y),'mx') >> subplot(1,2,1); >> plot(x,y,'go',x,true) >> subplot(1,2,2); >> plot(x,abs(true-y),'mx') 14

Figure 1. The two plots from the first approximation >> clf >> h = h/2; >> x1 = 0:h:1; >> y1 = 0*x1; >> y1(1) = 1; >> for i=2:max(size(y1)), y1(i) = y1(i-1) + h/y1(i-1); >> true1 = sqrt(2*x1+1); >> plot(x,y1,'go',x,true1)??? Error using ==> plot Vectors must be the same lengths. >> plot(x1,y1,'go',x1,true1) >> plot(x1,abs(true1-y1),'mx') >> subplot(1,2,1); >> plot(x,abs(true-y),'mx') >> subplot(1,2,2); >> plot(x1,abs(true1-y1),'mx') >> title('errors for h=1/32') 15

>> xlabel('x'); >> ylabel(' Error '); >> subplot(1,2,1); >> xlabel('x'); >> ylabel(' Error '); >> title('errors for h=1/16') Figure 2. The errors for the two approximations file: simpleeuler.m This matlab file will find the approximation to 16

dy/dx = 1/y y(0) = starty To run this file you will first need to specify the step the following: h : the step size starty : the initial value The routine will generate three vectors. The first vector is x which is the grid points starting at x0=0 and have a step size h. The second vector is an approximation to the specified D.E. The third vector is the true solution to the D.E. If you haven't guessed, you cna use the percent sign to add comments. x = [0:h:1]; y = 0*x; y(1) = starty; for i=2:max(size(y)), y(i) = y(i-1) + h/y(i-1); true = sqrt(2*x+1); >> simpleeuler??? Undefined function or variable h. Error in ==> /home/black/math/mat/examples/simpleeuler.m On line 27 ==> x = [0:h:1]; >> h = 1/16; 17

>> starty = 1; >> starty = 1; >> simpleeuler >> whos Name Size Elements Bytes Density Complex h 1 by 1 1 8 Full No i 1 by 1 1 8 Full No starty 1 by 1 1 8 Full No true 1 by 17 17 136 Full No x 1 by 17 17 136 Full No y 1 by 17 17 136 Full No Grand total is 54 elements using 432 bytes >> plot(x,y,'rx',x,true) >> x0 = x; >> y0 = y; >> true0 = true; >> h = h/2; >> simpleeuler >> whos Name Size Elements Bytes Density Complex h 1 by 1 1 8 Full No i 1 by 1 1 8 Full No starty 1 by 1 1 8 Full No true 1 by 33 33 264 Full No true0 1 by 17 17 136 Full No x 1 by 33 33 264 Full No x0 1 by 17 17 136 Full No y 1 by 33 33 264 Full No y0 1 by 17 17 136 Full No Grand total is 153 elements using 1224 bytes >> plot(x0,abs(true0-y0),'gx',x,abs(true-y),'yo'); 18

function [x] = gausselim(a,b) function [x] = gausselim(a,b) File gausselim.m This subroutine will perform Gaussian elmination on the matrix that you pass to it. i.e., given A and b it can be used to find x, Ax = b To run this file you will need to specify several things: A - matrix for the left hand side. b - vector for the right hand side The routine will return the vector x. ex: [x] = gausselim(a,b) this will perform Gaussian elminiation to find x. N = max(size(a)); Perform Gaussian Elimination for j=2:n, for i=j:n, m = A(i,j-1)/A(j-1,j-1); A(i,:) = A(i,:) - A(j-1,:)*m; b(i) = b(i) - m*b(j-1); 19

Perform back substitution x = zeros(n,1); x(n) = b(n)/a(n,n); for j=n-1:-1:1, x(j) = (b(j)-a(j,j+1:n)*x(j+1:n))/a(j,j); >> A = [1 2 3 6; 4 3 2 3; 9 9 1-2; 4 2 2 1] A = 1 2 3 6 4 3 2 3 9 9 1-2 4 2 2 1 >> b = [1 2 1 4]' b = 1 2 1 4 >> [x] = gausselim(a,b) x = >> 0.6809-0.8936 1.8085-0.5532 function [x,y] = eulerapprox(startx,h,x,starty,func) file: eulerapprox.m This matlab subroutine will find the approximation to 20

a D.E. given by y' = func(x,y) y(startx) = starty To run this file you will first need to specify the following: startx : the starting value for x h : the step size x : the ing value for x starty : the initial value func : routine name to calculate the right hand side of the D.E.. This must be specified as a string. ex: [x,y] = eulerapprox(0,1,1/16,1,'f'); Will return the approximation of a D.E. where x is from 0 to 1 in steps of 1/16. The initial value is 1, and the right hand side is calculated in a subroutine given by f.m. The routine will generate two vectors. The first vector is x which is the grid points starting at x0=0 and have a step size h. The second vector is an approximation to the specified D.E. x = [startx:h:x]; y = 0*x; y(1) = starty; for i=2:max(size(y)), y(i) = y(i-1) + h*feval(func,x(i-1),y(i-1)); function [f] = f(x,y) Evaluation of right hand side of a differential equation. f = 1/y; 21

>> help eulerapprox file: eulerapprox.m This matlab subroutine will find the approximation to a D.E. given by y' = func(x,y) y(startx) = starty To run this file you will first need to specify the following: startx : the starting value for x h x : the step size : the ing value for x starty : the initial value func : routine name to calculate the right hand side of the D.E.. This must be specified as a string. ex: [x,y] = eulerapprox(0,1,1/16,1,'f'); Will return the approximation of a D.E. where x is from 0 to 1 in steps of 1/16. The initial value is 1, and the right hand side is calculated in a subroutine given by f.m. The routine will generate two vectors. The first vector is x which is the grid points starting at x0=0 and have a step size h. The second vector is an approximation to the specified D.E. >> [x,y] = eulerapprox(0,1/16,1,1,'f'); >> plot(x,y) 22

decision = 3; leftx = 0; rightx = 1; lefty = 1; righty = 1; N= 10; h = (rightx-leftx)/(n-1); x = [leftx:h:rightx]'; A = zeros(n); for i=2:n-1, A(i,i-1:i+1) = [1-2 1]; A = A/h^2; A(1,1) = 1; A(N,N) = 1; b = sin(x); b(1) = lefty; b(n) = righty; if(decision<3) Find and plot the eigen values [e,v] = eig(a); e = diag(e); plot(real(e),imag(e),'rx'); title('eigen Values of the matrix'); elseif(decision>3) Find and plot the eigen values of inv(a) 23

[e,v] = eig(inv(a)); e = diag(e); plot(real(e),imag(e),'rx'); title('eigen Values of the inverse of the matrix'); else Solve the system y = A\b; linear = (lefty-righty+sin(leftx)-sin(rightx))/(leftx-rightx); constant = lefty + sin(leftx) - linear*leftx; true = -sin(x) + linear*x + constant; subplot(1,2,1); plot(x,y,'go',x,true,'y'); title('true Solution and Approximation'); xlabel('x'); ylabel('y'); subplot(1,2,2); plot(x,abs(y-true),'cx'); title('error'); xlabel('x'); ylabel(' Error '); 24