简单绘图.docx

上传人:b****4 文档编号:4519562 上传时间:2023-05-07 格式:DOCX 页数:13 大小:19.58KB
下载 相关 举报
简单绘图.docx_第1页
第1页 / 共13页
简单绘图.docx_第2页
第2页 / 共13页
简单绘图.docx_第3页
第3页 / 共13页
简单绘图.docx_第4页
第4页 / 共13页
简单绘图.docx_第5页
第5页 / 共13页
简单绘图.docx_第6页
第6页 / 共13页
简单绘图.docx_第7页
第7页 / 共13页
简单绘图.docx_第8页
第8页 / 共13页
简单绘图.docx_第9页
第9页 / 共13页
简单绘图.docx_第10页
第10页 / 共13页
简单绘图.docx_第11页
第11页 / 共13页
简单绘图.docx_第12页
第12页 / 共13页
简单绘图.docx_第13页
第13页 / 共13页
亲,该文档总共13页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

简单绘图.docx

《简单绘图.docx》由会员分享,可在线阅读,更多相关《简单绘图.docx(13页珍藏版)》请在冰点文库上搜索。

简单绘图.docx

简单绘图

1.簡介

2.簡單繪圖

3.程式撰寫

4.代數數學

1.

簡介

1.1何謂matlab?

Matrixlaboratory

1.2,基本矩陣表示法:

.用中括號[]表示矩陣開始與結束;

.用逗號或空白鍵區別矩陣中的元素

.用分號或是換行鍵來區別每列的結束

.向量表示法:

x=起始值:

增加值:

結束值

.單一元素呼叫:

x(n,m)

1.3.逗號和分號皆代表結束敘述,但分號不會顯示結果

!

執行外部DOS命令

ctrl+c中斷執行

NaN沒有意義的數

Inf無限大的數

Eps非常小的數

Pi圓周率

1.4.矩陣(matrix)和陣列(arrary)的區分

+和–通用

matrix:

*/^

array:

.*./.^(加上一個.號)

.運算時常會搞亂,要注意

1.5.虛數的輸入

i和j的被用來代表虛數根號–1

如:

z=3+4j或z=3+5*j

.i和j最好不要用來當做其他變數

1.6.一些矩陣的相關知識:

矩陣的合併與分割:

a=[a;r];a=a(1:

3;4:

5)

矩陣的轉置:

a=a’(ps:

會改變虛數正負號)

1.7.who,whos察看變數

what察看現在目錄

load,save資料存取

clf清除螢幕

clear清除所有變數

1.8.輸入與輸出

x=input(‘pleaseinputamatrix’)

x=input(‘pleaseinputastring’,’s’)

k=menu(‘pleaseinputyourchoice’,’choice1’,’choice2’,’choice3’)

fprintf(‘filename’,’thechoiceis%d%d%f,’var1’,’var2’,’var3’)

(ps:

若不設filename代表直接列印到螢幕)

1.9.程式流程控制

(1)forn=1:

1:

10

statement;

end

(2)whilen~=10

statemant;

end

(3).ifcondition

statement;

elseif

statement;

else

statemnet;

end

1.10.M檔案與M函數

二者皆為M檔案,但M函數必需在頭一行加上

function[output1,output2….]=A(input1,input2….)

例如

function[tot,M,N]=summtx(a)

tot=0;

[M,N]=size(a);

fori=1:

1:

N

forj=1:

1:

M

tot=tot+a(j,i);

end

end

儲存此檔案為summtx.m

1.11.help看副函式用法

lookfor查詢相關副函式

1.12.開檔與讀檔

fid=fopen(‘filename’,’permission’)

a=fread(fid,[mn]);

Forexample:

fid=fopen('杜甫詩.txt','r');

A=fread(fid);%Aisanarrayofintegers

S=char(A');%tochinesecharacter

fid2=fopen('test.txt','w');

fprintf(fid2,'%c',S);

fclose(fid2);

1.13.影像檔案相關指令

[a,map]=imread('filename');

imwrite(map,'filename');

imshow(a,map);

1.14.繪圖指令

plot(x),plot(x,y)將點用線連起來

stem(x)脈衝圖

figure開新圖

clf清除圖面

subplot

clear清除參數

1.15.path設定

path(path,'newpath')

 

2.

簡單繪圖

迴圈

FORI=1:

N,

FORJ=1:

N,

A(I,J)=1/(I+J-1);

END

END

EXAMPLE1

I=1;

forx=-pi:

0.01:

pi%i=-pitopi,step=0.01

y(I)=cos(i);%y(t)=cos(t)

I=I+1;

end

x=-pi:

0.01:

pi;

plot(x,y,'r');

xlabel('xinput');

ylabel('output');

title('coswaveform');

EXAMPLE1-1

x=-pi:

0.01:

pi;

y=cos(x);

plot(x,y,'r');

xlabel('xinput');

ylabel('output');

title('coswaveform');

EXAMPLE2(trysubplot)

x=-pi:

0.01:

pi;

y=cos(x);

z=sin(2*x);

subplot(2,1,1);

plot(x,y,'r');

xlabel('xinput');

ylabel('output');

title('cosxwaveform');

subplot(2,1,2);

plot(x,z,'g');

xlabel('xinput');

ylabel('output');

title('sin2xwaveform');

EXAMPLE3(tryholdon)

I=1;

x=-pi:

0.01:

pi

y=cos(x);

plot(x,y,'r');

xlabel('xinput');

ylabel('output');

title('cos(x)waveform');

holdon

z=sin(2*x);

plot(x,y,'k');

xlabel('xinput');

ylabel('output');

title('sin(2x)waveform');

 

EXAMPLE4(trycombination)

x=-pi:

0.01:

pi

y1=sin(x);

y2=cos(x);

plot(x,y1,'r',x,y2,'k');

xlabel('xinput');

ylabel('output');

title('sinandcoswaveform');

 

EXAMPLE5(trypolar)

r=2*(cos(theta)+1);

theta=0:

0.01:

2*pi;

polar(theta,r);

title('sweetheart');

3.程式撰寫

functionmean=avg(x,n)%MEANsubfunction

mean=sum(x)/n;

(tousetheotherwaytosovle.)

functionx=eqsol(A,B)%AisNbyNmatrixandBis1byNmatrix

x=inv(A)*B

functionx=meqsol(A,B)%AisNbyNmatrixandBisNbymatrix

[a,b]=size(A);

[c,d]=size(B);

if(b~=a)error('column#!

=raw#');end

if(d~=1)error('column#inthe2ndmatrixmustbe1');end

if(b~=c)error('column#inthe1stmatrixmustbeequaltoraw#inB');end

if(det(A)==0)error('1stmatrixdetermination=0');end

x=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='x^2+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-i*3^(1/2)]]'

solve(eq4,eq5,eq6)

ans=

x=-2,y=5,z=-6

tousetheotherwaytosovle.

functionx=eqsol(A,B)%AisNbyNmatrixandBis1byNmatrix

x=inv(A)*B

functionx=meqsol(A,B)%AisNbyNmatrixandBis1byNmatrix

[a,b]=size(A);

[c,d]=size(B);

if(b~=a)error('column#!

=raw#');end

if(d~=1)error('column#inthe2ndmatrixmustbe1');end

if(b~=c)error('column#inthe1stmatrixmustbeequaltoraw#inB');end

if(det(A)==0)error('1stmatrixdetermination=0');end

x=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','condition'),對應上述常微分方程式的符號運算式為:

soln_1=dsolve('Dy=3*t^2','y

(2)=0.5')

ans=

t^3-7.500000000000000

ezplot(soln_1,[2,4])

soln_2=dsolve('Dy=2*t*cos(y)^2','y(0)=pi/4')

ans=

atan(t^2+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對預設獨立變數的一次微分值

diff(f,'t')傳回f對獨立變數t的一次微分值

diff(f,n)傳回f對預設獨立變數的n次微分值

diff(f,'t',n)傳回f對獨立變數t的n次微分值

S1='6*x^3-4*x^2+b*x-5';

S2='sin(a)';

S3='(1-t^3)/(1+t^4)';

diff(S1)

ans=

18*x^2-8*x+b

diff(S1,2)

ans=

36*x-8

diff(S1,'b')

ans=

x

diff(S2)

ans=

cos(a)

diff(S3)

ans=

-3*t^2/(1+t^4)-4*(1-t^3)/(1+t^4)^2*t^3

simplify(diff(S3))

ans=

t^2*(-3+t^4-4*t)/(1+t^4)^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*x^3-4*x^2+b*x-5';

S2='sin(a)';

S3='sqrt(x)';

int(S1)

ans=

3/2*x^4-4/3*x^3+1/2*b*x^2-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.0741

Homework

(1):

inversetheinputorder

functiony=order_inv(x)%inversetheinputorder

len=length(x);

fori=1:

1:

len

y(len-i+1)=x(i);

end

Homework

(2):

findmaxandminvaluesinasequenceofx

function[z,y]=max_min(x)

%findmaxandminvaluesinasequenceofx

y=min(x);

z=max(x);

 

(thefollowingkeyingisinthematlabcommandwindows)

x=[1235-8-0.3]

[z,y]=max_min(x)

z=5

y=-8

附錄:

杜甫詩.txt

烽火連三月

家書抵萬金

白頭騷更短

渾欲不勝簪

國破山河在

城春草木深

感時花濺淚

恨別鳥驚心

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

当前位置:首页 > 临时分类 > 批量上传

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2