简单绘图Word下载.docx
《简单绘图Word下载.docx》由会员分享,可在线阅读,更多相关《简单绘图Word下载.docx(13页珍藏版)》请在冰点文库上搜索。
.運算時常會搞亂,要注意
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;
(3).ifcondition
elseif
else
statemnet;
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:
N
forj=1:
M
tot=tot+a(j,i);
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,'
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:
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;
x=-pi:
pi;
plot(x,y,'
xlabel('
xinput'
ylabel('
output'
title('
coswaveform'
EXAMPLE1-1
y=cos(x);
EXAMPLE2(trysubplot)
z=sin(2*x);
subplot(2,1,1);
plot(x,y,'
cosxwaveform'
subplot(2,1,2);
plot(x,z,'
g'
sin2xwaveform'
EXAMPLE3(tryholdon)
pi
cos(x)waveform'
holdon
k'
sin(2x)waveform'
EXAMPLE4(trycombination)
y1=sin(x);
y2=cos(x);
plot(x,y1,'
x,y2,'
);
sinandcoswaveform'
EXAMPLE5(trypolar)
r=2*(cos(theta)+1);
theta=0:
2*pi;
polar(theta,r);
sweetheart'
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#'
if(d~=1)error('
column#inthe2ndmatrixmustbe1'
if(b~=c)error('
column#inthe1stmatrixmustbeequaltoraw#inB'
if(det(A)==0)error('
1stmatrixdetermination=0'
x=inv(A)*B
一般方程式
以符號數學解一般方程式和聯立方程式的語法如下:
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)
x=-2,y=5,z=-6
tousetheotherwaytosovle.
functionx=meqsol(A,B)%AisNbyNmatrixandBis1byNmatrix
常微分方程式
y'
=3x2,y
(2)=0.5
=2.x.cos(y)2,y(0)=0.25
=3y+exp(2x),y(0)=3
dsolve('
equation'
condition'
),對應上述常微分方程式的符號運算式為:
soln_1=dsolve('
Dy=3*t^2'
y
(2)=0.5'
)
t^3-7.500000000000000
ezplot(soln_1,[2,4])
soln_2=dsolve('
Dy=2*t*cos(y)^2'
y(0)=pi/4'
atan(t^2+1)
soln_3=dsolve('
Dy=3*y+exp(2*t)'
y(0)=3'
-exp(2*t)+4*exp(3*t)
微分
diff函數用以演算一函數的微分項,相關的函數語法有下列4個:
diff(f)傳回f對預設獨立變數的一次微分值
diff(f,'
t'
)傳回f對獨立變數t的一次微分值
diff(f,n)傳回f對預設獨立變數的n次微分值
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)
18*x^2-8*x+b
diff(S1,2)
36*x-8
diff(S1,'
b'
x
diff(S2)
cos(a)
diff(S3)
-3*t^2/(1+t^4)-4*(1-t^3)/(1+t^4)^2*t^3
simplify(diff(S3))
t^2*(-3+t^4-4*t)/(1+t^4)^2
積分
int(f)傳回f對預設獨立變數的積分值
int(f,'
)傳回f對獨立變數t的積分值
int(f,a,b)傳回f對預設獨立變數的積分值,積分區間為[a,b],a和b為數值式
a,b)傳回f對獨立變數t的積分值,積分區間為[a,b],a和b為數值式
m'
n'
)傳回f對預設變數的積分值,積分區間為[m,n],m和n為符號式
sqrt(x)'
int(S1)
3/2*x^4-4/3*x^3+1/2*b*x^2-5*x
int(S2)
-cos(a)
int(S3)
2/3*x^(3/2)
int(S3,'
a'
2/3*b^(3/2)-2/3*a^(3/2)
int(S3,0.5,0.6)
2/25*15^(1/2)-1/6*2^(1/2)
numeric(int(S3,0.5,0.6))%使用numeric函數可以計算積分的數值
0.0741
Homework
(1):
inversetheinputorder
functiony=order_inv(x)%inversetheinputorder
len=length(x);
fori=1:
len
y(len-i+1)=x(i);
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
烽火連三月
家書抵萬金
白頭騷更短
渾欲不勝簪
國破山河在
城春草木深
感時花濺淚
恨別鳥驚心