matlab作业4

来源:互联网 发布:淘宝运营公司靠谱嘛 编辑:程序博客网 时间:2024/06/06 03:10

4.2

ii=-9:0.5:9;
t=ii >= 0;
y=-3.*ii(t).^2+5;
t=ii < 0;
y=3.*ii(t).^2+5;


4.4
subplot(2,1,1);
x=0.1:.1:3;
y=x.^2-3.*x+2;
plot(x,y,'r--','LineWidth',3);
subplot(2,1,2);
x=0.1:.1:3;
for>    y(i)=x(i)^2-3*x(i)+2; 
end
plot(x,y,'r--','LineWidth',3);


4.6
x=1;
for>    x = x+1;
end;
运行65537次


x=1;
for>    x = x+1;
end;
运行1次


x=1;
for>    x = x+1;
end;
运行2次


x=1;
for>    x = x+1;
end;
运行6次


4.8
a:
ires =>ires = ires + 1;
end
运行0次


b:
b.>while ires <= 200
ires =>while ires > 200
ires =>%for>%temp =>%y(ii) = temp(2);
%end
ii=1;
while>    temp = input('Enter [x y] pair: ');
   >    else 
        x(ii) =>        ii = ii+1;
       >    end;
end
sum_x = 0;
sum_y = 0;
sum_x2 = 0;
sum_xy = 0;
for>sum_x = sum_x + x(ii);
sum_y =>sum_xy = sum_xy + x(ii) * y(ii);
end
x_bar =>slope = (sum_xy - sum_x * y_bar) / ( sum_x2 - sum_x * x_bar);
y_int =>fprintf(' Intercept (b) = %8.3f\n', y_int);
fprintf(' No>plot(x,y,'bo');
hold>xmax = max(x);
ymin =>plot([xmin xmax],[ymin ymax],'r','LineWidth',2);
hold>while x<1
   >    fprintf('y=%f\n',y);
    x =>t=0:3:24;
i1=(2*i0).^t;
i2=(2*i0).^(t/1.5);
subplot(2,1,1);
plot(t,i1,'or-',t,i2,'ob--');
legend('A','B');
subplot(2,1,2);
loglog(t,i1,'or-',t,i2,'ob--');
legend('A','B');
ia24=(2*i0)^24;
ib24=(2*i0)^(24/1.5);
fprintf('24小时之后A、B培养基的病毒数分别为%d,%d',ia24,ib24);

4.10
我们可以用logical 函数给一个数组加上一个逻辑属性。
一个数组的逻辑属性可以通任何的数学运算去除。


4.12
加速度越小落地位移越长。
加速度变化对最佳抛出角度无影响。


4.14
disp('This program performs a leastsquares fit of an ');
disp('input data set to a straight line.');
%n_points = input('Enter the number of input [x y] points: ');
%for ii = 1:n_points
%temp = input('Enter [x y] pair: ');
%x(ii) = temp(1);
%y(ii) = temp(2);
%end
ii=1;
while ii ~=0
    temp = input('Enter [x y] pair: ');
    if(isempty(temp))
        ii = 0;
    else 
        x(ii) = temp(1);
        y(ii) = temp(2);
        ii = ii+1;
        n_points =ii-1;
    end;
end
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
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;
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 = %8d\n', n_points);
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.16




4.18
x = input('input number x<1');
while x<1
    y= log(1/(1-x));
    fprintf('y=%f\n',y);
    x = input('input number x<1: ');
end


4.22
i0=1;
t=0:3:24;
i1=(2*i0).^t;
i2=(2*i0).^(t/1.5);
subplot(2,1,1);
plot(t,i1,'or-',t,i2,'ob--');
legend('A','B');
subplot(2,1,2);
loglog(t,i1,'or-',t,i2,'ob--');
legend('A','B');
ia24=(2*i0)^24;
ib24=(2*i0)^(24/1.5);
fprintf('24小时之后A、B培养基的病毒数分别为%d,%d',ia24,ib24);


4.24
x=input('请输入一个数:(以负数结束输入)');
sum=0;
n=0;
m=1;
while(x>=0)
   >    m=m*x;
    x=input('请输入下一个数:');
   >end
ss=sum/n;
jh=m^(1/n);
fprintf('算术平均数为%f\n几何平均数为%f\n',ss,jh);


4.26
x=input('请输入一个数:(以负数结束输入)');
sum=0;
n=0;
m=1;
ds=0;
while(x>=0)
   >    m=m*x;
   >    x=input('请输入下一个数:');
   >end
th=n/ds;
ss=sum/n;
jh=m^(1/n);
fprintf('算术平均数为%f\n几何平均数为%f\n调和平均数为',ss,jh,th);


4.28
x=input('请输入设备的组成部分数量');
while x<=0
   >end
ds=0;
for>    fprintf('请输入第%d部分的平均无故障时间',ii);
   >    ds=1/subsystem+ds;
end
MTBF=1/ds;
fprintf('设备平均无故障时间为%f',MTBF);
0 0
原创粉丝点击