当前位置:文档之家› 计算物理

计算物理

本内容及供参考

实验一

第一部分

5、>> a1=[5 12 47;13 41 2;9 6 72];

>> a2=[12 9;6 5;7 21];

>> a1*a2

ans =

461 1092

416 364

648 1623

>> a1(:,1:2).*a2

ans =

60 108

78 205

63 126

>> a1^2

ans =

604 834 3643

616 1849 837

771 786 5619 >> a1(:).^2

ans =

25

169

81

144

1681

36

2209

4

5184

7、>> b1=[a1;rot90(a2)]

b1 =

5 12 47

13 41 2

9 6 72

9 5 21

12 6 7

>> c1=b1(4,1)

c1 =

9

>> c2=b1(5,3)

c2 =

7

>> b1(3:4,:)*a2

ans =

648 1623

285 547

13、>> d1=1:16;

>> d2=reshape(d1,4,4);

>> d3=fliplr(d2);

>> c=rot90(d3)

c =

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

>> c^(-4)

ans =

1.0e+010 *

0.5286 -1.7180 1.8502 -0.6608

-1.5858 3.4359 -2.1145 0.2644

1.5858 -1.7180 -1.3215 1.4537

-0.5286 0.0000 1.5858 -1.0572 >> (c^3)^(-1)

ans =

1.0e+012 *

-0.3665 0.5500 -0.0005 -0.1830

0.5498 -0.5502 -0.5490 0.5494

0 -0.5498 1.0995 -0.5498

-0.1833 0.5499 -0.5500 0.1834 >> (3*c+5*c^(-1))/5

ans =

1.0e+015 *

3.9406 -

4.5036 -2.8147 3.3777

-4.1283 4.5036 3.3777 -3.7530

-3.5653 4.5036 1.6888 -2.6271

3.7530 -

4.5036 -2.2518 3.0024 18、>> a=[1 2 3];

>> b=[2 3 4];

>> dot(a,b)

ans =

20

>> cross(a,b)

ans =

-1 2 -1

第二部分

6、>> syms x

>> expand((x+1)^3+(x-1)^3-2*x^3)

ans =

6*x

7、>> syms x

>> diff(log(cos(1/x))+(x^2-2*x^2+3)*exp(-x)+sin(x^3),1)

ans =

sin(1/x)/x^2/cos(1/x)-2*x*exp(-x)-(-x^2+3)*exp(-x)+3*cos(x^3)*x^2 >> diff(log(cos(1/x))+(x^2-2*x^2+3)*exp(-x)+sin(x^3),2)

ans =

-1/x^4-2*sin(1/x)/x^3/cos(1/x)-sin(1/x)^2/x^4/cos(1/x)^2-2*exp(-x)+4*x*exp(-x)+(-x^2+3) *exp(-x)-9*sin(x^3)*x^4+6*cos(x^3)*x

8、(1)>> syms x y dz dx dy

>> A=diff(sin(x*cos(y)),'x',1)

A =

cos(x*cos(y))*cos(y)

>> B=diff(sin(x*cos(y)),'y',1)

B =

-cos(x*cos(y))*x*sin(y)

>> dz=A*dx+B*dy

dz =

cos(x*cos(y))*cos(y)*dx-cos(x*cos(y))*x*sin(y)*dy

(2)>> syms x y z du dx dy dz

>> a=diff(asin(z/sqrt(x^2+y^2)),'x',1)

a =

-z/(x^2+y^2)^(3/2)*x/(1-z^2/(x^2+y^2))^(1/2)

>> b=diff(asin(z/sqrt(x^2+y^2)),'y',1)

b =

-z/(x^2+y^2)^(3/2)*y/(1-z^2/(x^2+y^2))^(1/2)

>> c=diff(asin(z/sqrt(x^2+y^2)),'z',1)

c =

1/(x^2+y^2)^(1/2)/(1-z^2/(x^2+y^2))^(1/2)

>> du=a*dx+b*dy+c*dz

du =

-z/(x^2+y^2)^(3/2)*x/(1-z^2/(x^2+y^2))^(1/2)*dx-z/(x^2+y^2)^(3/2)*y/(1-z^2/(x^2+y^

2))^(1/2)*dy+1/(x^2+y^2)^(1/2)/(1-z^2/(x^2+y^2))^(1/2)*dz

9、>> syms x a

>> symsum(a/(x^2),x,1,10)

ans =

1968329/1270080*a

>> symsum(a/(x^2),x,1,inf)

ans =

1/6*a*pi^2

10、>> syms n

>> symsum(3^(n+1)/(n+1),'n',1,5)

ans =

4077/20

>> symsum(5^(n+1)/(n+1),'n',1,5)

ans =

41275/12

11、>> syms x a

>> taylor(sin(x^2)+cos(x),5,'x',0)

ans =

1+1/2*x^2+1/24*x^4

>> taylor(sin(x^2)+cos(x),5,'x',3)

ans =

sin(9)+cos(3)+(6*cos(9)-sin(3))*(x-3)+(-1/2*cos(3)+cos(9)-18*sin(9))*(x-3)^2+(1/6*sin(

3)-36*cos(9)-6*sin(9))*(x-3)^3+(-18*cos(9)+107/2*sin(9)+1/24*cos(3))*(x-3)^4

>> taylor(log(x+a),5,'x',0)

ans =

log(a)+1/a*x-1/2/a^2*x^2+1/3/a^3*x^3-1/4/a^4*x^4

>> taylor(log(x+a),5,'x',3)

ans =

log(3+a)+1/(3+a)*(x-3)-1/2/(3+a)^2*(x-3)^2+1/3/(3+a)^3*(x-3)^3-1/4/(3+a)^4*(x-3)^4 >> taylor(sinh(x),5,'x',0)

ans =

x+1/6*x^3

>> taylor(sinh(x),5,'x',3)

ans =

1/2*exp(3)-1/2/exp(3)+(1/2*exp(3)+1/2/exp(3))*(x-3)+(-1/4/exp(3)+1/4*exp(3))*(x-3)^2 +(1/12/exp(3)+1/12*exp(3))*(x-3)^3+(1/48*exp(3)-1/48/exp(3))*(x-3)^4

实验二

第一部分

1、>>x1=-5:0.01:6;y1=0;

y2=-6:0.01:3;x2=0;

x3=-3:0.01:5;y3=1.5-0.5*x3;

plot(x1,y1,x2,y2,x3,y3)

或者

>> x1=-5:0.01:6;y1=0;

plot(x1,y1)

hold on

y2=-6:0.01:3;x2=0;

plot(x2,y2)

hold on

x3=-3:0.01:5;y3=1.5-0.5*x3;

plot(x3,y3)

或者

>> x=-5:0.01:6;y=0;

plot(x,y)

hold on

y=-6:0.01:3;x=0;

plot(x,y)

hold on

x=-3:0.01:5;y=1.5-0.5*x3;

plot(x,y)

图像

2、>> X=[-1 0 1 -1];

Y=[0 1 0 0];

plot(X,Y,'r--+','linewidth',3) 图像

3(1)

>> ezplot('y^2-2*x-1')

hold on

ezplot('y-x+4')

hold on

ezplot('y-x^2')

hold on

ezplot('x^2+y^2-1')

图像

3、(2)

>> clf

ezplot('6*x^2+5*y^2-30')

hold on

ezplot('(x^2+y^2)^2-x^2+y^2')

图像

3、(3)

>> ezplot('x^2+y^2+x-sqrt(x^2+y^2)') hold on

t=0:0.01:pi;

x=2*(cos(t)).^3;

y=2*(sin(t)).^3;

plot(x,y)

图像

4、a=-3;b=13;n=1000;

h=(b-a)/n;x=a;

for i=1:n

if x<0

y=4*(cos(x-pi/4))^2;

else if x<5

y=x/5+4;

else

y=x*exp(5-x);

end

X(i)=x;Y(i)=y;

x=x+h;

end

plot(X,Y)

图像

或者

a=-3;b=13;n=1000;

h=(b-a)/n;

for i=1:n

x(i)=a+h*(i-1);

if x(i)<0

y(i)=4*(cos(x(i)-pi/4))^2;

else if x(i)<5

y(i)=x(i)/5+4;

else

y(i)=x(i)*exp(5-x(i));

end

end

end

plot(x,y)

图像

5、

fplot('cos(tan(pi*x))',[0 2],1e-3) 图像

6、ezplot('exp(y)+cos(x)/x+y')

图像

第二部分

3、

>> [x y z]=ellipsoid(0,0,1,2,3,4,40);

subplot(1,2,1);

surf(x,y,z);

t=-1:0.01:1;

[x y z]=cylinder(1+t.^2,40);

subplot(1,2,2);

surf(x,y,z);

图像

综合题

1、A=1;fi=0;

w1=1;w2=2;

t=0:pi/100:2*pi;

x1=A*sin(w1*t+fi);

x2=A*sin(w2*t+fi);

plot(t,x1,t,x2)

text(2.5,0.7,'x1=A*sin(w1*t+fi)');

text(5,0.6,'x2=A*sin(w2*t+fi)');

legend('x1','x2')

图像

2、

clear

clf

A=1;w=1;

fi3=pi/6;fi4=pi/3;

t=0:pi/100:2*pi;

x3=A*sin(w*t+fi3);

x4=A*sin(w*t+fi4);

plot(t,x3,t,x4)

text(2.5,0.7,'x3=sin(t+pi/6)');

text(5,0.6,'x4=sin(t+pi/3)');

legend('x3','x4')

图像

3、

clear

clf

A5=1;A6=2;

w=1;fi=0;

t=0:pi/100:2*pi;

x5=A5*sin(w*t+fi); x6=A6*sin(w*t+fi); plot(t,x5,t,x6)

text(1,0.5,'x5=sin(t)'); text(3,1,'x6=2*sin(t)'); legend('x5','x6')

图像

实验五

龙格库塔法

二阶

a=2;b=4;

n=1000;

h=(b-a)/n;

y2(1)=1;x2(1)=a;

for i=1:n

k=h*f(x2(i),y2(i));

y2(i+1)=y2(i)+h*f(x2(i)+h/2,y2(i)+h/2);

x2(i+1)=x2(i)+h;

end

t=2:0.1:4;

T=exp(2-t.^2/2);

plot(t,T,'*',x2,y2)

图像

三阶

clear

clf

a=2;b=4;

n=1000;

h=(b-a)/n;

y3(1)=1;x3(1)=a;

for i=1:n

k1=h*f(x3(i),y3(i));

k2=h*f(x3(i)+h/2,y3(i)+k1/2);

k3=h*f(x3(i)+h,y3(i)-k1+2*k2);

y3(i+1)=y3(i)+(k1+4*k2+k3)/6;

x3(i+1)=x3(i)+h;

end

t=2:0.1:4;

T=exp(2-t.^2/2);

plot(t,T,'*',x3,y3)

图像

四阶

clear

clf

a=2;b=4;

n=1000;

h=(b-a)/n;

y4(1)=1;x4(1)=a;

for i=1:n

k1=h*f(x4(i),y4(i));

k2=h*f(x4(i)+h/2,y4(i)+k1/2);

k3=h*f(x4(i)+h/2,y4(i)+k2/2);

k4=h*f(x4(i)+h,y4(i)+k3);

y4(i+1)=y4(i)+(k1+2*k2+2*k3+k4)/6;

x4(i+1)=x4(i)+h;

end

t=2:0.1:4;

T=exp(2-t.^2/2);

plot(t,T,'*',x4,y4)

图像

2、Numerov算法

clear

clf

a=0;b=1;N=1000;h=(b-a)/N;

y(1)=2;y(2)=2;x(1)=a;x(2)=a+h;

for n=2:N

y(n+1)=2*(1-5*h^2/12*4*pi*pi)/(1+h^2/12*4*pi*pi)*y(n)-y(n-1); x(n+1)=x(n)+h;

end

plot(x,y)

图像

相关主题
文本预览
相关文档 最新文档