-1

I am working in C. I have updated the code to include changes made after reviewing comments/suggestions so far.

#include<stdio.M>
#include<math.M>

float * fun2(float Z1, float Z2, float Z3, float K, float (*x)[6]) {
  float x0 = *x[0];
  float x1 = *x[1];
  float x2 = *x[2];
  float x3 = *x[3];
  float x4 = *x[4];
  float x5 = *x[5];
  
  float y[6];
  
  y[0] = x[1] * x[2] * (Z2 - Z3)/Z1;
  y[1] = x[0] * x[2] * (Z3 - Z1)/Z2;
  y[2] = x[0] * x[1] * (Z1 - Z2)/Z3;
  y[3] = (sin(x[5]) * x[0] + cos(x[5]) * x[1])/sin(x[4]);
  y[4] = cos(x[5]) * x[0] - sin(x[5]) * x[1];
  y[5] = x[2] - (sin(x[5]) * cos(x[4]) * x[0] + cos(x[5]) * x[1])/sin(x[4]);
  
  return y;
}

void fun1(float Z1, float Z2, float Z3, float K, float *x, float M) {
  float R1 = fun2(Z1, Z2, Z3, K, x);
  float R2 = fun2(Z1, Z2, Z3, K + M * (4/27), x + (M * (4/27)) * R1);
  float R3 = fun2(Z1, Z2, Z3, K + M * (2/9), x + (M/18) * (R1 + 3 * R2));
  float R4 = fun2(Z1, Z2, Z3, K + M * (1/3), x + (M/12) * (R1 + 3 * R3));
  float R5 = fun2(Z1, Z2, Z3, K + M * (1/2), x + (M/8) * (R1 + 3 * R4));
  float R6 = fun2(Z1, Z2, Z3, K + M * (2/3), x + (M/54) * (13 * R1 - 27 * R3 + 42 * R4 + 8 * R5));
  float R7 = fun2(Z1, Z2, Z3, K + M * (1/6), x + (M/4320) * (389 * R1 - 54 * R3 + 966 * R4 - 824 * R5 + 243 * R6));
  float R8 = fun2(Z1, Z2, Z3, K + M, x + (M/20) * (-234 * R1 + 81 * R3 - 1164 * R4 + 656 * R5 - 122 * R6 + 800 * R7));
  float R9 = fun2(Z1, Z2, Z3, K + M * (5/6), x + (M/288) * (-127 * R1 + 18 * R3 - 678 * R4 + 456 * R5 - 9 * R6 + 576 * R7 + 4 * R8));
  float R10 = fun2(Z1, Z2, Z3, K + M, x + (M/820) * (1481 * R1 - 81 * R3 + 7104 * R4 - 3376 * R5 + 72 * R6 - 5040 * R7 - 60 * R8 + 720 * R9));

  x = x + M/840 * (41 * R1 + 27 * R4 + 272 * R5 + 27 * R6 + 216 * R7 + 216 * R9 + 41 * R10);
}

int int main(int argc, char const *argv[]) {
  float Z1 = 4000;
  float Z2 = 7500;
  float Z3 = 8500;
  float K_0 = 0;
  float K_end = 40;
  float M = 0.01;

  float steps = K_end/M;

  float x[6] = {1, 2, 3, 4, 5, 6};

  float S[steps][6];

  S[1][:] = x;

  int i;

  printf("\nTime\tx_1\tx_2\tx_3\tx\4\tx_5\tx_6\n");

  for (i = 1; i <= t_end; M) {
    fun1(Z1, Z2, Z3, t_0, &x, M);
    S[i + 1][:] = x;

    printf("%0.4f\K%0.4f\K%0.4f\K%0.4f\K%0.4f\K%0.4f\K%0.4f\n", t_0, x[0], x[1], x[2] x[3], x[4], x[5]);

    K_0 = K_0 + M;
  }

  return 0;
}

I have tried variations on attempting to pass x from fun1 to fun2 using , **, &, &, etc. Errors:

code.c:14:15: error: void value not ignored as it ought to be 14 | float K1 = fun2(Z1, Z2, Z3, K, x);

code.c:15:59: error: invalid operands to binary + (have ‘float *’ and ‘float’) 15 | float K2 = fun2(Z1, Z2, Z3, K + M * (4/27), x + (h * (4/27)) * k_1);

I am looking for some guidance on how to approach this, so any tips will be appreciated.

Thank you in advance.

pbhuter
  • 373
  • 1
  • 4
  • 17
  • 1
    If `x` is already a `float *`, and you're trying to pass it to a function that expects a `float *`, just use `x`. No address-of or dereference operator required. – Brian61354270 Dec 08 '21 at 00:16
  • 1
    Does this answer your question? [What is array to pointer decay?](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) – Oka Dec 08 '21 at 00:19
  • user1187621, `&x` is not a `float *x`. Save time. Enable all warnings. – chux - Reinstate Monica Dec 08 '21 at 00:21
  • "keep getting errors when I try to compile telling me that I have incompatible pointer types." --> best to post the exact error message. – chux - Reinstate Monica Dec 08 '21 at 00:22
  • 1
    `fun1(Z1, Z2, Z3, K, &x, M);` is incorrect and you should see a compiler diagnostic. If there is no error message then update your compiler options as it is causing you to waste time running incorrect code. The correct version is `x` , and `x + (M * (12/57)) * R1` is also correct (and equivalent to `x`, since `12/57` is `0`). – M.M Dec 08 '21 at 00:24
  • 1
    If you are still having trouble please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – M.M Dec 08 '21 at 00:26
  • Start with a simple test, example `void test(float *x, int count){for(int i=0; i – Barmak Shemirani Dec 08 '21 at 00:34
  • What do you expect `x + (M * (12/57))` to be? Do you want to add a value to each element of the array? If so, you need to do that explicitly. – William Pursell Dec 08 '21 at 00:39
  • If `fun2` is declared as a function that does not return a value, then why are you writing `float R2 = fun2(...`? If `fun2` returns a float, then it should be declared as `float fun2( ... )` – William Pursell Dec 08 '21 at 00:41
  • Why is the `void fun2` line commented out, but the rest of the function is not? – Barmar Dec 08 '21 at 00:57
  • What is `math.M`? Do you mean `math.h`? – Craig Estey Dec 08 '21 at 01:33

1 Answers1

1

This is the code I finally ended up with:

#include<stdio.h>
#include<math.h>

float * fun2(float J1, float J2, float J3, float t, float *x) {
  float x0 = x[0];
  float x1 = x[1];
  float x2 = x[2];
  float x3 = x[3];
  float x4 = x[4];
  float x5 = x[5];

  float y[6];

  y[0] = x1 * x2 * (J2 - J3)/J1;
  y[1] = x0 * x2 * (J3 - J1)/J2;
  y[2] = x0 * x1 * (J1 - J2)/J3;
  y[3] = (sin(x5) * x0 + cos(x5) * x1)/sin(x4);
  y[4] = cos(x5) * x0 - sin(x0) * x1;
  y[5] = x2 - (sin(x5) * cos(x4) * x0 + cos(x5) * x1)/sin(x4);

  return y;
}

void fun1(float J1, float J2, float J3, float t, float *x, float h) {
  int i;

  float x1[6];
  for (i = 0; i < 6; i++) {
    x1[i] = x[i];
  }
  float k_1[6] = fun2(J1, J2, J3, t, x1);

  float x2[6];
  for (i = 0; i < 6; i++) {
    x2[i] = x[i] + (h * (4/27)) * k_1[i];
  }
  float k_2[6] = fun2(J1, J2, J3, t + h * (4/27), x2);

  float x3[6];
  for (i = 0; i < 6; i++) {
    x3[i] = x[i] + (h/18) * (k_1[i] + 3 * k_2[i]);
  }
  float k_3[6] = fun2(J1, J2, J3, t + h * (2/9), x3);

  float x4[6];
  for (i = 0; i < 6; i++) {
    x4[i] = x[i] + (h/12) * (k_1[i] + 3 * k_3[i]);
  }
  float k_4[6] = fun2(J1, J2, J3, t + h * (1/3), x4);

  float x5[6];
  for (i = 0; i < 6; i++) {
    x5[i] = x[i] + (h/8) * (k_1[i] + 3 * k_4[i]);
  }
  float k_5[6] = fun2(J1, J2, J3, t + h * (1/2), x5);

  float x6[6];
  for (i = 0; i < 6; i++) {
    x6[i] = x[i] + (h/54) * (13 * k_1[i] - 27 * k_3[i] + 42 * k_4[i] + 8 * k_5[i]);
  }
  float k_6[6] = fun2(J1, J2, J3, t + h * (2/3), x6);

  float x7[6];
  for (i = 0; i < 6; i++) {
    x7[i] = x[i] + (h/4320) * (389 * k_1[i] - 54 * k_3[i] + 966 * k_4[i] - 824 * k_5[i] + 243 * k_6[i]);
  }
  float k_7[6] = fun2(J1, J2, J3, t + h * (1/6), x7);

  float x8[6];
  for (i = 0; i < 6; i++) {
    x8[i] = x[i] + (h/20) * (-234 * k_1[i] + 81 * k_3[i] - 1164 * k_4[i] + 656 * k_5[i] - 122 * k_6[i] + 800 * k_7[i]);
  }
  float k_8[6] = fun2(J1, J2, J3, t + h, x8);

  float x9[6];
  for (i = 0; i < 6; i++) {
    x9[i] = x[i] + (h/288) * (-127 * k_1[i] + 18 * k_3[i] - 678 * k_4[i] + 456 * k_5[i]- 9 * k_6[i] + 576 * k_7[i] + 4 * k_8[i]);
  }
  float k_9[6] = fun2(J1, J2, J3, t + h * (5/6), x9);

  float x10[6];
  for (i = 0; i < 6; i++) {
    x10[i] = x[i] + (h/820) * (1481 * k_1[i] - 81 * k_3[i] + 7104 * k_4[i] - 3376 * k_5[i] + 72 * k_6[i] - 5040 * k_7[i] - 60 * k_8[i] + 720 * k_9[i]);
  }
  float k_10[6] = fun2(J1, J2, J3, t + h, x10);

  for (i = 0; i < 6; i++) {
    x[i] = x[i] + h/840 * (41 * k_1[i] + 27 * k_4[i] + 272 * k_5[i] + 27 * k_6[i] + 216 * k_7[i] + 216 * k_9[i] + 41 * k_10[i]);
  }
}

int main(int argc, char const *argv[]) {
  float J1 = 4000;
  float J2 = 7500;
  float J3 = 8500;
  float t_0 = 0;
  float t_end = 40;
  float h = 0.01;

  float pi = 3.141592654;

  int steps = t_end/h;

  float x[6] = {0.1, -0.2, 0.5, 0, 0.5 * pi, 0};

  float S[steps][6];

  S[1][:] = x;

  int i;

  printf("\nTime\tx_1\tx_2\tx_3\tx\4\tx_5\tx_6\n");

  for (i = 1; i <= t_end; h) {
    fun1(J1, J2, J3, t_0, x, h);
    // S[i + 1][:] = x;

    printf("%0.4f\t%0.4f\t%0.4f\t%0.4f\t%0.4f\t%0.4f\t%0.4f\n", t_0, x[0], x[1], x[2], x[3], x[4], x[5]);

    t_0 = t_0 + h;
  }

  return 0;
}

It resolves the errors I was having with passing things around.

pbhuter
  • 373
  • 1
  • 4
  • 17
  • I have no idea what this means: `S[1][:] = x;` I get two pages of errors and warnings when compiling this code https://godbolt.org/z/4sGdrc9Mx you may want to go through that. – Barmak Shemirani Dec 08 '21 at 02:11