收藏 分享(赏)

一般方程式.doc

上传人:HR专家 文档编号:5926179 上传时间:2019-03-21 格式:DOC 页数:22 大小:61.50KB
下载 相关 举报
一般方程式.doc_第1页
第1页 / 共22页
一般方程式.doc_第2页
第2页 / 共22页
一般方程式.doc_第3页
第3页 / 共22页
一般方程式.doc_第4页
第4页 / 共22页
一般方程式.doc_第5页
第5页 / 共22页
点击查看更多>>
资源描述

1、1. 簡介2. 簡單繪圖3. 程式撰寫4. 代數數學1. 簡介1.1 何謂 matlab? Matrix laboratory1.2,基本矩陣表示法:用中括號 表示矩陣開始與結束;用逗號或空白鍵區別矩陣中的元素用分號或是換行鍵來區別每列的結束向量表示法:x=起始值:增加值:結束值單一元素呼叫:x(n,m)1.3.逗號和分號皆代表結束敘述,但 分號 不會顯示結果! 執行外部 DOS 命令ctrl+c 中斷執行NaN 沒有意義的數Inf 無限大的數Eps 非常小的數Pi 圓周率1.4.矩陣(matrix)和陣列 (arrary)的區分+ 和 通用matrix: * / array: .* ./ .

2、 (加上一個 . 號)運算時常會搞亂,要注意1.5.虛數的輸入i 和 j 的被用來代表虛數根號 1如: z=3+4j 或 z=3+5*j和最好不要用來當做其他變數1.6.一些矩陣的相關知識:矩陣的合併與分割:a=a;r ; a=a(1:3;4:5)矩陣的轉置:a=a (ps:會改變虛數正負號)1.7.who ,whos 察看變數what 察看現在目錄load, save 資料存取clc 清除螢幕clear 清除所有變數1.8.輸入與輸出x = input(please input a matrix)x = input(please input a string,s)k = menu(pleas

3、e input your choice , choice1,choice2,choice3)fprintf(filename,the choice is %d %d %f,var1,var2,var3)(ps:若不設 filename 代表直接列印到螢幕)1.9.程式流程控制(1) for n=1:1:10statement;end(2) while n=10statemant;end(3). if conditionstatement;elseifstatement;else statemnet;end1.10.M 檔案與 M 函數二者皆為 M 檔案, 但 M 函數必需在頭一行加上outpu

4、t1,output2.=function(input1,input2.)%註解符號的添加pause1.11.help 看副函式用法lookfor 查詢相關副函式1.12.開檔與讀檔fid = fopen(filename,permission)a=fread(fid,m n);For example:fid = fopen(杜甫詩.txt,r);A = fread(fid); % A is an array of integersS = char(A); % to chinese characterfid2 = fopen(test.txt,w);fprintf(fid2,%c,S);fclo

5、se(fid2);1.13.影像檔案相關指令a,map=bmpread(filename);bmpwrite(map,filename);imshow(a,map);1.14.繪圖指令plot(x),plot(x,y) 將點用線連起來stem(x) 脈衝圖figure 開新圖clg 清圖subplot 1.15.path 設定path(path ,new path)2. 簡單繪圖迴圈FOR I = 1:N,FOR J = 1:N,A(I,J) = 1/(I+J-1);ENDENDEXAMPLE 1I = 1;for i = -pi:0.01:pi % i = -pi to pi, step =

6、 0.01y(I) = cos(i); % y (t) = cos(t) I = I+1;endx = -pi:0.01:piplot(x,y,r);xlabel( x input);ylabel( output );title( cos waveform );EXAMPLE 2 ( try subplot)I = 1;for i = -pi:0.01:piy(I) = cos(i);I = I+1;endx = -pi:0.01:pisubplot(2,1,1); % subplot(1,2,1)plot(x,y,r);xlabel( x input);ylabel( output );ti

7、tle( cos waveform );I = 1;for i = -pi:0.01:piy(I) = sin(i);I = I+1;endx = -pi:0.01:pisubplot(2,1,2); %subplot(1,2,2)plot(x,y,k);xlabel( x input);ylabel( output );title( sin waveform );EXAMPLE 3 (try hold on)I = 1;for i = -pi:0.01:piy(I) = cos(i);I = I+1;endx = -pi:0.01:piplot(x,y,r);xlabel( x input)

8、;ylabel( output );title( cos waveform );hold on I = 1;for i = -pi:0.01:piy(I) = sin(i);I = I+1;endx = -pi:0.01:piplot(x,y,k);xlabel( x input);ylabel( output );title( sin waveform );EXAMPLE 4 (try combination)I = 1;for i = -pi:0.01:piy1(I) = cos(i);y2(I) = sin(i);I = I+1;endx = -pi:0.01:piplot(x,y,r,

9、 x,y,k);xlabel( x input);ylabel( output );title( sin and cos waveform ); EXAMPLE 5 (try polar )I = 1;for i = 0:0.01:2*pir(I) = 2*(cos(i) +1 );I = I+1;endtheta = 0:0.01:2*pi;polar(theta, r);title( sweet heart );EXAMPLE 6 (directly)x = 0:0.01:2*pi;y = 2*(cos(x) +1);polar(x,y);title(sweet heart);EXAMPL

10、E 7 ( modified)x = 0:0.01:1.0;y = sin(2*pi*x);plot(x,y)title( y = sin(2*pi*x) );xlabel(time);ylabel(output); 3. 程式撰寫function mean = avg(x,n) %MEAN subfunctionmean = sum(x)/n;(to use the other way to sovle.)function x = eqsol(A, B) % A is N by N matrix and B is 1 by N matrixx = inv(A)*Bfunction x = m

11、eqsol(A, B) % A is N by N matrix and B is N by matrixa, b = size(A);c, d = size(B);if( b = a) error( column # != raw #); endif( d = 1 ) error( column # in the 2nd matrix must be 1); endif( b = c) error( column # in the 1st matrix must be equal to raw # in B); endif( det(A) = 0) error(1st matrix dete

12、rmination = 0); endx = inv(A)*B 4. 代數數學一般方程式以符號數學解一般方程式和聯立方程式的語法如下: solve(f) 解符號方程式f。 solve(f1,fn) 解由f1, ,fn組成的聯立方程式。 eq1 = x-3=4; % 注意也可寫成eq1=x-7 eq2 = x*2-x-6=0; % 注意也可寫成 eq2=x*2-x-6 eq3 = x2+2*x+4=0; eq4 = 3*x+2*y-z=10; eq5 = -x+3*y+2*z=5; eq6 = x-y-z=-1; solve(eq1) solve(eq3) ans= -1+i*3(1/2),-1

13、-i*3(1/2) solve(eq4,eq5,eq6)ans= x = -2, y = 5, z = -6to use the other way to sovle.function x = eqsol(A, B) % A is N by N matrix and B is 1 by N matrixx = inv(A)*Bfunction x = meqsol(A, B) % A is N by N matrix and B is 1 by N matrixa, b = size(A);c, d = size(B);if( b = a) error( column # != raw #);

14、 endif( d = 1 ) error( column # in the 2nd matrix must be 1); endif( b = c) error( column # in the 1st matrix must be equal to raw # in B); endif( det(A) = 0) error(1st matrix determination = 0); endx = inv(A)*B 常微分方程式y=3x2, y(2)=0.5 y=2.x.cos(y)2, y(0)=0.25 y=3y+exp(2x), y(0)=3 dsolve(equation,cond

15、ition),對應上述常微分方程式的符號運算式為: soln_1 = dsolve(Dy = 3*t2,y(2)=0.5) ans= t3-7.500000000000000 ezplot(soln_1,2,4) soln_2 = dsolve(Dy = 2*t*cos(y)2,y(0) = pi/4) ans= atan(t2+1) soln_3 = dsolve(Dy = 3*y + exp(2*t), y(0) = 3) ans= -exp(2*t)+4*exp(3*t) 微分diff 函數用以演算一函數的微分項,相關的函數語法有下列 4 個: diff(f) 傳回 f 對預設獨立變數的

16、一次微分值 diff(f,t) 傳回 f 對獨立變數 t 的一次微分值 diff(f,n) 傳回 f 對預設獨立變數的 n 次微分值 diff(f,t,n) 傳回 f 對獨立變數 t 的 n 次微分值 S1 = 6*x3-4*x2+b*x-5; S2 = sin(a); S3 = (1 - t3)/(1 + t4); diff(S1) ans= 18*x2-8*x+b diff(S1,2) ans= 36*x-8 diff(S1,b) ans= x diff(S2) ans= cos(a) diff(S3) ans= -3*t2/(1+t4)-4*(1-t3)/(1+t4)2*t3 simpl

17、ify(diff(S3) ans= t2*(-3+t4-4*t)/(1+t4)2 積分int(f) 傳回 f 對預設獨立變數的積分值 int(f,t) 傳回 f 對獨立變數 t 的積分值 int(f,a,b) 傳回 f 對預設獨立變數的積分值,積分區間為a,b,a 和 b 為數值式 int(f,t,a,b) 傳回 f 對獨立變數 t 的積分值,積分區間為a,b,a 和 b 為數值式 int(f,m,n) 傳回 f 對預設變數的積分值,積分區間為m,n,m 和 n 為符號式 S1 = 6*x3-4*x2+b*x-5; S2 = sin(a); S3 = sqrt(x); int(S1) ans=

18、 3/2*x4-4/3*x3+1/2*b*x2-5*x int(S2) ans= -cos(a) int(S3) ans= 2/3*x(3/2) int(S3,a,b) ans= 2/3*b(3/2)- 2/3*a(3/2) int(S3,0.5,0.6) ans= 2/25*15(1/2)-1/6*2(1/2) numeric(int(S3,0.5,0.6) % 使用 numeric 函數可以計算積分的數值 ans= 0.0741Home work (1): inverse the input orderfunction y = order_inv(x) % inverse the inpu

19、t orderlen = length(x);for i = 1:1:leny(len-i+1) = x(i);endHome work (2): find max and min values in a sequence of xfunction z, y = max_min(x) % find max and min values in a sequence of xy = min(x);z = max(x);(the following keying is in the matlab command windows)x = 1 2 3 5 -8 -0.3z, y = max_min(x)z = 5y = -8附錄: 杜甫詩.txt烽火連三月家書抵萬金白頭騷更短渾欲不勝簪國破山河在城春草木深感時花濺淚恨別鳥驚心

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 经营企划

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报