1. 首页
  2. 考试认证
  3. 其它
  4. 附加的画图特性-大学物理知识点总结

附加的画图特性-大学物理知识点总结

上传者: 2024-07-27 06:14:10上传 PDF文件 52.68MB 热度 7次

第三章分支语句和编程设计3.5附加的画图特性第70页的答案。根据下面的描述编写对应的MATLAB语句。

  1. 如果x大于等于0,把x的平方根赋值于变量sqrt_x,并打印出结果。否则,打印出一条关于平方根函数参数的错误信息,并把sqrt_x归零。

if x >= 0

    sqrt_x = sqrt(x);

    disp(sqrt_x);

else

    disp('Error: Negative input for square root function.');

    sqrt_x = 0;

end

  1. 变量fun由n/m计算得到,如果m的绝对值小于1.0e-300,打印出除数为0,否则计算并打印出fun值。

if abs(m) < 1.0e-300

    disp('Error: Divisor is zero.');

else

    fun = n / m;

    disp(fun);

end

  1. 租用一个交通工具前100公里0.50美元每公里,在下面的200公里中0.30美元每公里,越过300公里的部分一律按0.20美元每公里。已知公里数,编写对应的MATLAB语句计算出总费用和平均每公里的费用。

distance = %已知公里数;

if distance <= 100

    cost = distance * 0.5;

elseif distance <= 300

    cost = 100 * 0.5 + (distance - 100) * 0.3;

else

    cost = 100 * 0.5 + 200 * 0.3 + (distance - 300) * 0.2;

end

avg_cost = cost / distance;

disp(['Total cost: ', num2str(cost)]);

disp(['Average cost per km: ', num2str(avg_cost)]);

  1. 检测下面的MATLAB语句,是对是错?正确的,输出结果如何,错误的,错在哪里?

if volts > 125

    disp('WARNING: High voltage on line.');

if volts < 105

    disp('WARNING: Low voltage on line.');

else

    disp('Line voltage is within tolerances.');

end

这个语句是错误的,因为内层的if缺少end,导致结构混乱。应该修改为:


if volts > 125

    disp('WARNING: High voltage on line.');

elseif volts < 105

    disp('WARNING: Low voltage on line.');

else

    disp('Line voltage is within tolerances.');

end


color = 'yellow';

switch(color)

    case 'red'

        disp('Stop now!');

    case 'yellow'

        disp('Prepare to stop.');

    case 'green'

        disp('Proceed through intersection.');

    otherwise

        disp('Illegal color encountered.');

end

这个语句是正确的,输出Prepare to stop.

6.


if temperature > 37

    disp('Human body temperature exceeded.');

elseif temperature > 100

    disp('Boiling point of water exceeded.');

end

这个语句是错误的。因为当温度大于100时,条件temperature > 37也为真,因此不会进入elseif。应该调整为:


if temperature > 100

    disp('Boiling point of water exceeded.');

elseif temperature > 37

    disp('Human body temperature exceeded.');

end

3.5附加的画图特性在本节中,我们将讨论简单的二维图象(在第二章我们已有所介绍)的附加特性。这些特性将允许我们控制x,y轴上的值的范围,在一个坐标系内打印多个图象,或创建多个图,或在一个图象窗口内创建多个子图像,或提供更加强大的轨迹文本字符控制。还有,我们将学习如何创建极坐标。

3.5.1控制x,y轴绘图的上下限在默认的情况下,图象的X,Y轴的范围宽到能显示输入值的每一个点。但是有时只显示这些数据的一部分非常有用,这时你可以应用axis命令/函数。 axis命令/函数的一些形式展示在表3.5中。其中两个最重要的形式在表中用黑体字标出——它允许程序员设定和修改坐标的上下限。所有形式的完全列表将会在MATLAB的在线文件中找到。为了说明axis的应用,我们将画出函数f(x)=sinx从­2π到2π之间的图象,然后限定坐标的上下限。

想深入了解MATLAB控制语句?可以查看这里!更多关于MATLAB画图命令的详细内容,也可参考这篇文档。如果你对Python中的分支语句与循环语句应用感兴趣,请访问这个链接

用户评论