c语言递归求差分方程,matlab利用递归求解差分方程

matlab利用递归求解差分方程

2018-5-23来自ip:15.17.168.197的网友咨询

浏览量:168

问题补充:

matlab利用递归求解差分方程

function y = recur(a,b,n,x,x0,y0);

%

% y = recur(a,b,n,x,x0,y0)

% solves for y[n] from:

% y[n] + a1*y[n-1] + a2*y[n-2]...+ an*y[n-N]

% = b0*x[n] + b1*x[n-1] + ...+ bm*x[n-M]

%

% a,b,n,x,x0 and y0 are vectors

% a = [a1 a2 ...aN]

% b = [b0 b1 ...bM]

% n contains the time values for which the solution will be computed

% y0 contains the initial conditions for y,in order,

% i.e.,y0 = [y[n0-N],y[n0-N+1],...,y[n0-1]]

% where n0 represents the first element of n

% x0 contains the initial conditions on x,in order

% i.e.,x0 = [x[n0-M],...,x[n0-1]]

% the output,y,has length(n)

%

N = length(a);

M = length(b)-1;

y = [y0 zeros(1,length(n))];

x = [x0 x]

a1 = a(length(a):-1:1) % reverses the elements in a

b1 = b(length(b):-1:1)

for i=N+1:N+length(n),

y(i) = -a1*y(i-N:i-1)' + b1*x(i-N:i-N+M)';

end

y = y(N+1:N+length(n))

看到循环那有些看不明白

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言递归求解二元不定方程组的思路如下: 1. 先将方程组化为标准形式,即x1 + y1 = c1,x2 + y2 = c2。 2. 定义一个递归函数,输入参数为当前处理的方程组和已经出的解。 3. 若当前方程组已经为空,则输出已出的解。 4. 否则,取出第一个方程,假设为x1 + y1 = c1。 5. 对于x1,枚举其可能的取值,假设为i。 6. 将x1 = i 代入方程 x1 + y1 = c1 中,得到 y1 = c1 - i。 7. 将 y1 = c1 - i 代入剩下的方程组中,得到一个新的方程组。 8. 对新的方程组进行递归调用,输入为新的方程组和已出的解加上 (i, c1-i)。 9. 递归结束后,输出已出的解。 以下是一个示例代码: ```c #include <stdio.h> void solve(int a[][3], int n, int m, int x[], int dep) { if (n == 0) { printf("(%d,%d)\n", x[0], x[1]); return; } int b[10][3]; int y[2]; int i, j; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { b[i][j] = a[i][j]; } } y[0] = b[0][2] - b[0][0] * x[0]; y[1] = b[0][2] - b[0][1] * x[1]; for (i = 0; i <= y[0]; i++) { x[0] = i; x[1] = y[1]; solve(b + 1, n - 1, m, x, dep + 1); } } int main() { int a[][3] = {{2, 1, 5}, {1, -1, 1}}; int x[2] = {0, 0}; solve(a, 2, 3, x, 0); return 0; } ``` 在上面的代码中,a表示输入的方程组,n表示方程组的个数,m表示每个方程的系数和常数的个数(本例中是3),x表示已经出的解,dep表示当前递归深度。在递归调用solve函数时,输入的方程组为b + 1,即去掉第一个方程后的方程组,已出的解为x加上(i, c1-i)。当递归深度达到方程组个数时,即已出所有解,输出即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值