1

我是机械工程师,C 语言的初学者

想使用 Summation 函数对 1x14 矩阵 Cx1 Cy1 Cz1 中的所有分量求和

我也想计算 (Cx1 * transpose(Ma))/sum(Ma) (得到质心)

任何人都可以帮助我吗?我只习惯于使用 matlab,所以使用函数或指针让我很困惑(我使用的是 WiringPi,因为我必须使用 Raspberry pi 运行它......)

#include <stdio.h>
#include <wiringPi.h>
#include <stdlib.h>
#include <errno.h>
#include <wiringSerial.h>
#include <math.h>


void MatrixMultiply(int m, int p, int n, float A[m][p], float B[p][n], float C[p][n])   
{

int i, j, k;
for (i=0;i<m;i++)
   for(j=0;j<n;j++)
      {
      C[i][j]=0;
      for (k=0;k<p;k++)
         C[i][j]= C[i][j]+A[i][k]*B[k][j];
      }
}


void Summation(int m,int n, double Cx[n], double Cy[n], double  Cz[n], double M[n])
{
int i;

double sum_x =0;
double sum_y =0;
double sum_z =0;
double CG_x1 = 0;
double CG_y1 = 0;
double CG_z1 = 0;
double Mass = 0;



double Cx1[14] = {-0.00037435797,0,0,0.00118574942,3.07214914879,0,-3.53615559888,3.53615559888,-3.49598511933,-0.58036525258,-2.58027392955,0,3.53315689833,-0.00339553944};
double Cy1[14] = {-0.00091507094,0,0,0.00067588546,1.79688158586,0.37033689898,4.68812966408,-4.68812966408,1.38566975835,-3.72451299038,-1.48972184781,0,1.67480858582,-0.00196041565};
double Cz1[14] = {0.39494859708,-0.1525,5.91074892102,2.82120389989,4.13903790555 ,2.00112984375,4.79000000005 ,4.79000000005,3.20714937209,3.22445881918 ,1.38480905325,0.85250000000,11.28209081559,9.29000000000};
double Ma[14] =  {0.55884245503, 0.60366974489, 3.95197090345, 20.82750506085, 2.46150714667, 0.31070393824, 0.64982394170, 0.64982394170, 2.53800582113,2.53800582113, 2.50769704699, 0.86840073405, 0.17496883559, 0.56740282314, 0.6989733086 };


for(i = 0;i<m;i++)
{
sum_x += Cx1[i];
sum_y += Cy1[i];
sum_z += Cz1[i];
Mass += M[i];
}


}




double main(void)
{
double sum_x =0;
double sum_y =0;
double sum_z =0;
double CG_x1 = 0;
double CG_y1 = 0;
double CG_z1 = 0;
double Mass = 0;




Summation(15,14,double Cx1,double Cy1,double Cz1,double Ma);
MatrixMUltiply(?) 
printf("x = %15.15f\n, y = %15.15f\n, z = %15.15f\n Mass = %15.15f\n",sum_x,sum_y,sum_z};

}
4

1 回答 1

0

这是在 C 中进行矩阵乘法的示例程序:http: //www.programiz.com/c-programming/examples/matrix-multiplication-function

你也可以在这里学习 C 教程:http: //www.w3schools.in/c-tutorial/

于 2016-11-05T00:34:16.390 回答