matlab作业参考4,matlab第四章作业
4.15 修改4.7
中的程序lsqfit,使它能夠從input1.dat
文件中讀取它的輸入值。文件中的數(shù)據(jù)是以行組織的,每一行都有一對(duì)(x,y),如下所示:
1.1 2.2
2.2 3.3
...
用例4.7 中的數(shù)據(jù)檢測(cè)你的程序。(提示:我們用load
命令從input1 數(shù)組讀數(shù)據(jù)。然后把input1 的第一列賦值于數(shù)組x,把input1
的第二列賦值于數(shù)組y)。
解答:
disp('This
program performs a leastsquares fit of an ');
disp('input
data set to a straight line.');
n_points =
load('sample_file.txt')
% Read the
input data
for ii =
1:n_points
temp =
input('Enter [x y] pair: ');
x(ii) =
temp(1);
y(ii) =
temp(2);
end
% Accumulate
statistics
sum_x =
0;
sum_y =
0;
sum_x2 =
0;
sum_xy =
0;
for ii =
1:n_points
sum_x = sum_x
+ x(ii);
sum_y = sum_y
+ y(ii);
sum_x2 =
sum_x2 + x(ii)^2;
sum_xy =
sum_xy + x(ii) * y(ii);
end
% Now
calculate the slope and intercept.
x_bar = sum_x
/ n_points;
y_bar = sum_y
/ n_points;
slope =
(sum_xy - sum_x * y_bar) / ( sum_x2 - sum_x * x_bar);
y_int = y_bar
- slope * x_bar;
% Tell
user.
disp('Regression coefficients
for the leastsquares line:');
fprintf('
Slope (m) = %8.3f\n', slope);
fprintf('
Intercept (b) = %8.3f\n', y_int);
fprintf(' No
of points = \n', n_points);
% Plot the
data points as blue circles with no
% connecting
lines.
plot(x,y,'bo');
hold
on;
xmin =
min(x);
xmax =
max(x);
ymin = slope *
xmin + y_int;
ymax = slope *
xmax + y_int;
plot([xmin
xmax],[ymin ymax],'r','LineWidth',2);
hold
off;
title
('\bfLeastSquaresFit');
xlabel('\bf\itx');
ylabel('\bf\ity');
legend('Input
data','Fitted line');
grid
on
4.17 在例4.3
中,已知年月日,計(jì)算相應(yīng)的thedayofyear。在這個(gè)程序中,并沒有檢測(cè)是否輸入了正確的年月日,它能接收無效的月和日,并產(chǎn)生無意義的結(jié)果。修改你的程序使之只能輸入有效的年月日。如果輸入的值無效,則提示用戶出錯(cuò),并且跳出執(zhí)行。我們要求年應(yīng)當(dāng)大于0月只能是1
到12 之間的整數(shù)。日只能1 到那一月的最大數(shù)之間的整數(shù)。用switch 結(jié)構(gòu)檢查日是否正確。
解答:
disp('This
program calculates the day of year given the ');
disp('current
date.');
month = input('Enter
current month (1-12):');
day = input('Enter
current day(1-31):');
year = input('Enter
current year(yyyy): ');
% Check for leap
year, and add extra day if necessary
if mod(year,400) ==
0
leap_day = 1; %
Years divisible by 400 are leap years
elseif mod(year,100)
== 0
leap_day = 0; %
Other centuries are not leap years
elseif mod(year,4)
== 0
leap_day = 1; %
Otherwise every 4th year is a leap year
else
leap_day = 0; %
Other years are not leap years
end
% Calculate day of
year by adding current day to the
% days in
previous months.
day_of_year =
day;
for ii =
1:month - 1
% Add days in
months from January to last month
switch
(ii)
case
{1,3,5,7,8,10,12},
day_of_year =
day_of_year + 31;
case
{4,6,9,11},
day_of_year =
day_of_year + 30;
case
2,
day_of_year =
day_of_year + 28 + leap_day;
end
end
% Tell
user
fprintf('The
date -/-/M is day of year %d.\n', ...
month, day,
year, day_of_year);
4.19 斐波那契數(shù)列。含有n
個(gè)數(shù)的斐波那契數(shù)列的定義如下:
f(1) = 1
f(2) = 2
f(n) = f(n-1) + f(n-2)
所以f(3)=f(2)+f(1)=2+1=3,還有更多的數(shù)。在M
文件中編寫一程序,計(jì)算并寫斐波那契數(shù)列中第n(n>2)個(gè)數(shù)的值,n 由用戶輸入。用for 循環(huán)進(jìn)行計(jì)算。
解答:
n=input('請(qǐng)輸入一個(gè)數(shù)n');
f(1)=1;
f(2)=2;
if(n>2)
for
i=3:n
f(i)=f(i-1)+f(i-2);
end
end
fprintf('f(%d)的值為%d',n,f(n));
4.21
輕繩上的拉力。一重200
英磅的物體被固定在一水平桿的末端。如圖4.5 所示這一水平桿由一輕繩固定。繩子上的拉力為
T 代表繩子的拉力,W 代表物體的重量,lp 代表?xiàng)U的長度,lc
為繩長,d 代表繩與桿的結(jié)點(diǎn)到墻面的距離。編寫一個(gè)程序,以確定d 為多大時(shí),繩的拉力最小。為達(dá)此目的,d
應(yīng)從1 英尺到7 英尺,每隔1
英尺取一次值,并找出使拉力最小的d。
4.23 分貝
工程師們經(jīng)常用分貝或dB 來描述兩功率之比。1dB
的定義如下,
P2 是已測(cè)量的功率,P1 代表參考功率。假設(shè)參考功率P1 是1
瓦。P2 從1 到20 瓦每隔0.5
瓦取一次值,編寫程序,計(jì)算相應(yīng)的dB 值,并畫出dB-P2
解答:
P1=1;
P2=1:0.5:20;
dB=10*log10(P2);
plot(P2,dB);
4.25
均方根平均數(shù)(rmsaverage)。均方根平均數(shù)是另一種計(jì)算數(shù)據(jù)平均數(shù)的方法。它的定義如下
編寫一個(gè)程序,它能接受任意個(gè)數(shù)的正輸入值,并計(jì)算它們的算太平均數(shù)和幾何平均數(shù)。用while
循環(huán)讀取輸入值,當(dāng)輸入一個(gè)負(fù)數(shù)中止輸入數(shù)據(jù)。計(jì)算數(shù)列10,5,2,5 的均方根平均數(shù),用以檢測(cè)程序。
解答:
x=input('請(qǐng)輸入一個(gè)數(shù):(以負(fù)數(shù)結(jié)束輸入)');
sum=0;
n=0;
m=1;
ad=0;
while(x>=0)
sum=sum+x;
m=m*x;
ad=ad+1/x;
x=input('請(qǐng)輸入下一個(gè)數(shù):');
n=n+1;
end
hm=n/ad;
ss=sum/n;
mm=m^(1/n);
fprintf('算術(shù)平均數(shù)為%f\n幾何平均數(shù)為%f\n調(diào)和平均數(shù)為',ss,mm,hm);
4.27
編寫一個(gè)程序,能夠計(jì)算一系列正數(shù)的算術(shù)平均數(shù),幾何平均數(shù),均方根平均數(shù),調(diào)和均數(shù)。可使用任意算法讀取輸入值。用下列數(shù)測(cè)試你的程序。
總結(jié)
以上是生活随笔為你收集整理的matlab作业参考4,matlab第四章作业的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php post收不到值,php 取不到
- 下一篇: 舍选法抽样matlab,12 重要抽样法