MATLAB练习例子十三(模拟计算1)
返回
% filename:test30.m
x = 0:.1:1;
y = [x; exp(x)];
fid = fopen('C:\matlab\exp.txt','w');
fprintf(fid,'%6.2f %12.8f \n',y);
fclose(fid)
exp.txt
0.00 1.00000000
0.10 1.10517092
0.20 1.22140276
0.30 1.34985881
0.40 1.49182470
0.50 1.64872127
0.60 1.82211880
0.70 2.01375271
0.80 2.22554093
0.90 2.45960311
1.00 2.71828183
% filename:test31.m
fid = fopen('C:\RVDZ\rv\exp.txt');
a = fscanf(fid, '%f %f',[2 inf]) % It has two rows now.
a = a';
fclose(fid)
》 test31
a =
Columns 1 through 7
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000
1.0000 1.1052 1.2214 1.3499 1.4918 1.6487 1.8221
Columns 8 through 11
0.7000 0.8000 0.9000 1.0000
2.0138 2.2255 2.4596 2.7183
z1 = fzero('func_1',[2,5])
y1 = func_1(z1)
》 test27
Zero found in the interval: [2, 5].
z1 =
3.8986
y1 =
0
function y = func_2(x)
y = exp(-0.5*x).*cos(5*x) - 0.4*exp(-0.3*x);
t = 0:0.1:10
y = func_2(t);
n = length(y);
k = 0;
for i = 1:n-1
if y(i)*y(i+1) < 0
k = k + 1;
z(k) = fzero('func_2',[t(i) t(i+1)]);
end
end
z
y = func_2(z)
fplot('func_2',[0 10])
grid on
function y = func_1(x)
y = -x^4 + 20*x^2 - 20*x + 5;
x1 = fmin('func_1',-2,2)
x1 =
0.5135
y1 = func_1(x1)
y1 =
-0.0659
》 test24
??? スクリプトM-file内で関数宣言は利用できません.
エラー: ==> C:\matlab\test24.m
行番号: 5 ==>function [a, b] = heikin(x)
% 必须单独列出!
function [a, b] = heikin(x)
% 求平均值,分布值, a:平均值 b:分布值
n = length(x); % x的元素
a = sum(x)/n; % x的总和
b = sum((x-a).^2)/n;
x = input('Enter x ');
[mean,sigmas] = heikin(x)
》 test24
Enter x [3 4 5 6 7 8 9]
mean =
6
sigmas =
4
p = [-1 0 20 -20 5]
r = roots(p)
p1 = poly(r)
y = polyval(p,1.5)
p =
-1 0 20 -20 5
r =
-4.9261
3.8986
0.5736
0.4539
p1 =
1.0000 -0.0000 -20.0000 20.0000 -5.0000
y =
14.9375
t = 0:0.1:10;
y = exp(-0.5*t)*cos(5*t);
plot(t,y)
grid on
??? エラー: ==> *
内部行列の次元は同じである必要があります.
エラー: ==> C:\RVDZ\rv\test20.m
行番号: 3 ==>y = exp(-0.5*t)*cos(5*t);
t = 0:0.1:10;
y = exp(-0.6*t).*cos(5*t);
plot(t,y)
grid on
k = input('Enter k ');
n = input('Enter n ');
x = -10:0.02:10;
y = zeros(size(x))
a = 1;
for i = 1:n
y = y + sin(a*x)/a;
a = a + 2;
end
y = y*4*k/pi;
plot(x,y)
grid on
p = [-1 0 20 -20 5];
x = -5:0.02:5;
y = polyval(p,x);
plot(x,y)
grid on
n = input('Enter n ');
x = 0:10;
y = [0 3 4 10 18 27 41 59 79 93 101];
p = polyfit(x,y,n)
x1 = 0:0.1:10;
y1 = polyval(p,x1);
plot(x,y,'o',x1,y1) % (x,y)数据用o显示
% (x1,y1)数据用线连接
grid on
Enter n 4
p =
-0.0405 0.6917 -2.3639 5.1253 -0.2937
function y = func_s(x)
y = sin(1/(abs(x)+0.05));
》 x=0:0.1:10;
》 y = func_s(x);
??? エラー: ==> /
行列の次元は同じである必要があります.
エラー: ==> C:\matlab\func_s.m
行番号: 2 ==>y = sin(1/(abs(x)+0.05));
》 fplot('func_s',[0 10])
》 fplot('func_s',[0 1])
》 fplot('func_s',[0 0.4])
》 fplot('func_s',[-0.4 0.4])
》 grid on
t = -0.4:0.1:0.4;
y = func_s(t);
n = length(y);
k = 0;
for i = 1:n-1
if y(i)*y(i+1) < 0
k = k + 1;
z(k) = fzero('func_s',[t(i) t(i+1)]);
end
end
z
fplot('func_s',[-0.4 0.4])
grid on
??? エラー: ==> /
行列の次元は同じである必要があります.
エラー: ==> C:\matlab\func_s.m
行番号: 2 ==>y = sin(1/(abs(x)+0.05));
返回