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 second ro
超级会员免费看
订阅专栏 解锁全文
1万+

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



