MATLAB编程:嵌套、函数传递与案例分析
1. 嵌套与缩进
在编程结构中,结构之间可以相互“嵌套”,即把一个结构放置在另一个结构内部。下面通过一个求解二次方程根的例子来说明这一概念。
1.1 问题描述
对于二次方程 (f(x) = ax^{2}+bx + c),其根可以通过二次公式 (x=\frac{-b\pm\sqrt{b^{2}-4ac}}{2a}) 来确定。我们的目标是开发一个函数,根据系数的值实现这个公式。
1.2 解决方案
采用自顶向下的设计方法,先构建通用结构,再细化算法。
- 通用结构 :
function quadroots(a, b, c)
% quadroots: roots of quadratic equation
% quadroots(a,b,c): real and complex roots
% of quadratic equation
% input:
% a = second-order coefficient
% b = first-order coefficient
% c = zero-order coefficient
% output:
% r1 = real part of first root
% i1 = imaginary part of first root
% r2 = real part of second root
% i2 = imaginary part of s
超级会员免费看
订阅专栏 解锁全文
1万+

被折叠的 条评论
为什么被折叠?



