what s the quetion
please use matlab to get all the graphs, an write the answer with logic clearly.
please notice each question and answer everyone.
and get all the graphs.
here is the sample code
/* This is a very primitive, standard c code [NOT FOR DISTRIBUTION] that will show the basics of a second order finite difference approximation to the wave eqaution without damping -- as described in the problem set. compile with cc -O3 -o run.job main_sample_MAT128C_4.c -lm or gcc -O3 -o run.job main_sample_MAT128C_4.c -lm then run the job with run.job or ./run.job */ #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <sys/types.h> #include <sys/times.h> #include<sys/param.h> #define BUF1 32 int main() { /* Declare variables */ int n_dt, n, j, mm; double *f_now, t0, tn, y0, *y_now, *y_new, *y_pre, eta; double v0, vn, vm, a, b, c1, c2, c3, c4, norm1, norm2; double t_start, dt, x_l, x_r, y_l, y_r, dx; /* This is for opening a file -- leave it as is unless you know what you are doing */ char *rundat = "Dat"; char datfile[BUF1]; FILE *f_dat; /* Declaration of right-hand side of differential equation */ extern double func(); /* Open the data file Dat -- leave it as is unless you know what you are doing */ sprintf(datfile, "%s", rundat); if ((f_dat = fopen(datfile, "w")) == NULL) { fprintf(stdout, "Can't open Dat filen"); fflush(stdout); exit(1); } /* Set system parameters */ eta = 0.5; mm = 1000; /* Number of spatial segments */ x_l = 0.0; /* Boundary Conditions */ x_r = 10.0; dx = (x_r-x_l)/mm; /* spatial segmentation */ y_l = asin(eta); y_r = y_l+2.0*M_PI; t_start = 0.0; /* initial time */ dt = 0.009; /* time step */ n_dt = 100000; /* maximum number of time steps */ /* Create arrays */ if ((y_pre = malloc((mm+1)*sizeof(double))) == NULL) { fprintf(stdout, "problems with allocating memory for y_pren"); fflush(stdout); exit(1); } if ((y_now = malloc((mm+1)*sizeof(double))) == NULL) { fprintf(stdout, "problems with allocating memory for y_nown"); fflush(stdout); exit(1); } if ((y_new = malloc((mm+1)*sizeof(double))) == NULL) { fprintf(stdout, "problems with allocating memory for y_newn"); fflush(stdout); exit(1); } if ((f_now = malloc((mm+1)*sizeof(double))) == NULL) { fprintf(stdout, "problems with allocating memory for f_nown"); fflush(stdout); exit(1); } /* Initial condition */ t0 = t_start; /* initial time */ tn = t0; for (j=0; j<=mm; j++) { y_pre[j] = 0.0; /* initialize for good order */ y_now[j] = y_l+(j*dx)*(y_r-y_l)/(x_r-x_l); /* line that satisfies BCs */ y_new[j] = y_l+(j*dx)*(y_r-y_l)/(x_r-x_l); /* line that satisfies BCs */ f_now[j] = 0.0; /* initialize for good order */ } /* now dump the initial function and its half-step velocity to the Dat file */ for (j=0; j<=mm; j++) { fprintf(f_dat, "%e %e %en", j*dx, y_new[j], (y_new[j]-y_now[j])/dt); } fprintf(f_dat, "n"); fflush(f_dat); /* Prepare for time-step loop */ /* set coefficients for method -- see below */ c1 = 2.0*(1.0-dt*dt/(dx*dx)); c2 = dt*dt/(dx*dx); c4 = dt*dt; /* enter the discrete-time loop of the finite difference map */ for (n = 1; n < n_dt; n++) { /* reset solution for new time step */ for (j=1; j<mm; j++) { y_pre[j] = y_now[j]; y_now[j] = y_new[j]; } /* calculate function f */ for (j=1; j<mm; j++) { f_now[j] = func(eta, y_now[j]); } norm1 = 0.0; norm2 = 0.0; /* do the time step */ for (j=1; j<mm; j++) { y_new[j] = c1*y_now[j]+c2*(y_now[j-1]+y_now[j+1])-y_pre[j]+c4*f_now[j]; norm1 += fabs(y_new[j]-y_now[j]); if (fabs(y_new[j]-y_now[j]) > norm2) norm2 = fabs(y_new[j]-y_now[j]); } norm1 /= dt*mm; norm2 /= dt; tn = t0+n*dt; /*time updated */ if (norm1 < 1.0e-10) n=n_dt; /* finish early if tolerance is met */ } for (j=0; j<=mm; j++) { fprintf(f_dat, "%e %e %en", j*dx, y_new[j], (y_new[j]-y_now[j])/dt); } fprintf(f_dat, "n"); fprintf(f_dat, "%e %e %en", tn, norm1, norm2); fprintf(stdout, "time=%e, norm1=%e, norm2=%en", tn, norm1, norm2); fflush(f_dat); fclose(f_dat); } /* the right-hand side function f */ double func(eta, y) double y, eta; { double f; f = -sin(y)+eta; return f; }