1

I am trying to re write my code implementing on a C compilator in MATLAB. The aim is to solve using finite volume method the equation below :

enter image description here

With :

enter image description here

I am applying the method such that :

enter image description here

  ## Code
mandatory declarations: */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

/** definition of the field h, the flux Q, time step */
double*x=NULL,*h=NULL,*Q=NULL;
double dt,L0,Delta;
double t;
int i,N;

/** Main with definition of parameters */
int main() {
  L0 = 5.;
  N = 128;
  t=0;
  Delta = L0/N;
  dt =.0025;

/**  dynamic allocation  */
  x= (double*)calloc(N+1,sizeof(double));
  h= (double*)calloc(N+1,sizeof(double));
  Q= (double*)calloc(N+1,sizeof(double));

/**
first cell between  ‘0-Delta/2‘ and ‘0+Delta/2‘, centred in
ith cell beween ‘(i-1/2) Delta‘ (left) and ‘(i +1/2) Delta‘(right) centered in ‘(i)
*/
  for(i=0;i<=N;i++)
    {  x[i]=0+(i)*Delta;
       h[i] = (1)*(x[i]<1);}

/**  begin the time loop */
   while(t<=100){
     t = t + dt;

/**      flux    */
 for(i=1;i<=N;i++)
    Q[i] =  - 1./3*pow(((h[i]+h[i-1])/2),3)*(h[i]-h[i-1])/Delta;

/**  explicit step  update and BC$$h_i^{n+1}=h_i^{n} -{\Delta t} \dfrac{F(Q_{i+1})-
 for(i=1;i<N-1;i++)
    h[i] +=  - dt* ( Q[i+1] - Q[i] )/Delta;
  h[0]=h[1];
  h[N]=h[N-1];
  }

/** clean */
  free(h);
  free(Q);
  free(x);
} /**

I am just wondering how to write the Q[I] line in MATLAB because h[I] depends on Q[I].

Thank you in advance,

KarlZ
  • 19
  • 3
  • I am not sure what the confusion is. You can write MATLAB code that looks almost exactly identical to the C code, with the loops and such. – Ander Biguri May 10 '22 at 10:21
  • @AnderBiguri I am not sure how to write the dynamic allocation on matlab `x= (double*)calloc(N+1,sizeof(double));` and the line `h[i] = (1)*(x[i]<1);` – KarlZ May 10 '22 at 10:53
  • 2
    You don't need to, but the correct way is just making an array with `zeros` as `x=zeros(N+1,1);` The other line is `h(i) =(1)*(x(i)<1);` I insist, what is the problem? have you started trying? – Ander Biguri May 10 '22 at 10:57
  • 1
    @AnderBiguri Thank you : I've succeeded to do it I was writing the indexes in a wrong way. – KarlZ May 11 '22 at 10:37

0 Answers0