matlab作业参考4,matlab第四章作业

a4c26d1e5885305701be709a3d33442f.png

4.15 修改4.7

中的程序lsqfit,使它能够从input1.dat

文件中读取它的输入值。文件中的数据是以行组织的,每一行都有一对(x,y),如下所示:

1.1 2.2

2.2 3.3

...

用例4.7 中的数据检测你的程序。(提示:我们用load

命令从input1 数组读数据。然后把input1 的第一列赋值于数组x,把input1

的第二列赋值于数组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

中,已知年月日,计算相应的thedayofyear。在这个程序中,并没有检测是否输入了正确的年月日,它能接收无效的月和日,并产生无意义的结果。修改你的程序使之只能输入有效的年月日。如果输入的值无效,则提示用户出错,并且跳出执行。我们要求年应当大于0月只能是1

到12 之间的整数。日只能1 到那一月的最大数之间的整数。用switch 结构检查日是否正确。

解答:

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 斐波那契数列。含有n

个数的斐波那契数列的定义如下:

f(1) = 1

f(2) = 2

f(n) = f(n-1) + f(n-2)

所以f(3)=f(2)+f(1)=2+1=3,还有更多的数。在M

文件中编写一程序,计算并写斐波那契数列中第n(n>2)个数的值,n 由用户输入。用for 循环进行计算。

解答:

n=input('请输入一个数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

a4c26d1e5885305701be709a3d33442f.png

轻绳上的拉力。一重200

英磅的物体被固定在一水平杆的末端。如图4.5 所示这一水平杆由一轻绳固定。绳子上的拉力为

a4c26d1e5885305701be709a3d33442f.png

T 代表绳子的拉力,W 代表物体的重量,lp 代表杆的长度,lc

为绳长,d 代表绳与杆的结点到墙面的距离。编写一个程序,以确定d 为多大时,绳的拉力最小。为达此目的,d

应从1 英尺到7 英尺,每隔1

英尺取一次值,并找出使拉力最小的d。

4.23 分贝

工程师们经常用分贝或dB 来描述两功率之比。1dB

的定义如下,

a4c26d1e5885305701be709a3d33442f.png

P2 是已测量的功率,P1 代表参考功率。假设参考功率P1 是1

瓦。P2 从1 到20 瓦每隔0.5

瓦取一次值,编写程序,计算相应的dB 值,并画出dB-P2

解答:

P1=1;

P2=1:0.5:20;

dB=10*log10(P2);

plot(P2,dB);

a4c26d1e5885305701be709a3d33442f.png

4.25

均方根平均数(rmsaverage)。均方根平均数是另一种计算数据平均数的方法。它的定义如下

a4c26d1e5885305701be709a3d33442f.png编写一个程序,它能接受任意个数的正输入值,并计算它们的算太平均数和几何平均数。用while

循环读取输入值,当输入一个负数中止输入数据。计算数列10,5,2,5 的均方根平均数,用以检测程序。

解答:

x=input('请输入一个数:(以负数结束输入)');

sum=0;

n=0;

m=1;

ad=0;

while(x>=0)

sum=sum+x;

m=m*x;

ad=ad+1/x;

x=input('请输入下一个数:');

n=n+1;

end

hm=n/ad;

ss=sum/n;

mm=m^(1/n);

fprintf('算术平均数为%f\n几何平均数为%f\n调和平均数为',ss,mm,hm);

4.27

编写一个程序,能够计算一系列正数的算术平均数,几何平均数,均方根平均数,调和均数。可使用任意算法读取输入值。用下列数测试你的程序。

a4c26d1e5885305701be709a3d33442f.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值