matlab ss2tf函数 C实现

继上一篇matlab zp2ss函数 C实现

这一篇实现ss2tf(将状态空间系统转换为传递函数形式)

代码

void ss2tf(double *a, double *b, double *c, double *d, int n, double *num, double *den)
{
    poly(a, n, den);

    double *temp_a = malloc(sizeof(double) * n * n);

    trmul(b, c, n, 1, n, temp_a);
    
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < n; j++)
        {
            temp_a[i * n + j] = a[i * n + j] - temp_a[i * n + j];
        }
    }

    poly(temp_a, n, num);//

    for (size_t i = 0; i < n + 1; i++)
    {
        num[i] += (*d - 1.0) * den[i];
    }
}

其中poly是矩阵求特征多项式的函数:

void poly(double *p, int np, double *d)
{
    double *temp = (double *)malloc(sizeof(double) * np * np);

    for (size_t i = 0; i < np; i++)
    {
        for (size_t j = 0; j < np; j++)
        {
            temp[i * np + j] = p[i * np + j];
        }
    }

    // eig(A)
    double *u_real = (double *)malloc(sizeof(double) * np);
    double *v_imag = (double *)malloc(sizeof(double) * np);
    int jt = 60;
    hhbg(temp, np);
    hhqr(temp, np, EPS, jt, u_real, v_imag);

    Complex *dt = (Complex *)malloc(sizeof(Complex) * (np + 1));
    Complex *dtt = (Complex *)malloc(sizeof(Complex) * (np + 1));
    dt[0].real = 1;
    dt[0].imaginary = 0;
    for (size_t i = 1; i < np + 1; i++)
    {
        dt[i].real = 0;
        dt[i].imaginary = 0;
    }
    for (size_t i = 0; i < np; i++)
    {
        for (size_t j = 1; j <= i + 1; j++)
        {
            dtt[j] = complex_subtract(dt[j], complex_multiply(create(u_real[i], v_imag[i]), dt[j - 1]));
        }
        for (size_t j = 1; j <= i + 1; j++)
        {
            dt[j].real = dtt[j].real;
            dt[j].imaginary = dtt[j].imaginary;
        }
    }

    for (size_t i = 0; i < np + 1; i++)
    {
        d[i] = dt[i].real;
    }

    free(dt);
    free(dtt);
}

trmul是两个矩阵相乘:

void trmul(double a[], double b[], int m, int n, int k, double c[])
{
    int i, j, l, u;
    for (i = 0; i <= m - 1; i++)
        for (j = 0; j <= k - 1; j++)
        {
            u = i * k + j;
            c[u] = 0.0;
            for (l = 0; l <= n - 1; l++)
                c[u] = c[u] + a[i * n + l] * b[l * k + j];
        }
}

测试

与matlab的ss2tf函数对比,基本一致,如果矩阵为病态条件矩阵则结果异常,需要注意。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值