#include #include "plot3d.h" static void plot_with_geomview(struct plot3d *data) { FILE *fp; int m = data->m; int n = data->n; double dx = (data->b - data->a) / n; double dt = data->T / m; fp = fopen(data->geomview_out, "w"); if (fp == NULL) { fprintf(stderr, "*** Cannot open Geomview script file %s " "for writing\n", data->geomview_out); return; } fprintf(fp, "# Geomview script writen by Math 625's finite difference solver\n"); fprintf(fp, "{ appearance { +edge }\n"); fprintf(fp, "MESH %d %d\n", n+1, m+1); for (int i = 0; i <= m; i++) { double t = i*dt; for (int j = 0; j <= n; j++) { double x = data->a + j*dx; fprintf(fp, "%g %g %g\n", x, t, data->u[i][j]); } } fprintf(fp, "}\n"); fclose(fp); printf("Geomview script written to %s\n", data->geomview_out); } static void plot_with_maple(struct plot3d *data) { FILE *fp; int m = data->m; int n = data->n; double dx = (data->b - data->a) / n; double dt = data->T / m; fp = fopen(data->maple_out, "w"); if (fp == NULL) { fprintf(stderr, "*** Cannot open Maple script file %s " "for writing\n", data->maple_out); return; } fprintf(fp, "# Maple script writen by Math 625's finite difference solver\n"); fprintf(fp, "plots:-display(MESH([\n"); for (int i = 0; i <= m; i++) { double t = i*dt; fprintf(fp, "\t["); for (int j = 0; j <= n; j++) { double x = data->a + j*dx; fprintf(fp, "[%g,%g,%g],", x, t, data->u[i][j]); } fprintf(fp, "NULL],\n"); } fprintf(fp, "NULL]), labels=[x,t,u]);"); fclose(fp); printf("Maple script written to %s\n", data->maple_out); } // names of matlab's plotting variables #define XVEC "PLOT_WITH_MATLAB_FD_XVEC" #define TVEC "PLOT_WITH_MATLAB_FD_TVEC" #define ZMAT "PLOT_WITH_MATLAB_FD_ZMAT" static void plot_with_matlab(struct plot3d *data) { FILE *fp; int m = data->m; int n = data->n; double dx = (data->b - data->a) / n; double dt = data->T / m; fp = fopen(data->matlab_out, "w"); if (fp == NULL) { fprintf(stderr, "*** Cannot open Matlab script file %s " "for writing\n", data->matlab_out); return; } fprintf(fp, "%% Matlab script writen by Math 625's finite difference solver\n"); fprintf(fp, "%s = [\n", XVEC); for (int j = 0; j <= n; j++) { double x = data->a + j*dx; fprintf(fp, "%g ", x); } fprintf(fp, "\n];\n"); fprintf(fp, "%s = [\n", TVEC); for (int i = 0; i <= m; i++) { double t = i*dt; fprintf(fp, "%g ", t); } fprintf(fp, "\n];\n"); fprintf(fp, "%s = [\n", ZMAT); for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) fprintf(fp, "%g ", data->u[i][j]); fprintf(fp, "\n"); } fprintf(fp, "];\n"); fprintf(fp, "surf(%s, %s, %s)\n", XVEC, TVEC, ZMAT); fprintf(fp, "xlabel('x')\n"); fprintf(fp, "ylabel('t')\n"); fprintf(fp, "zlabel('u')\n"); fprintf(fp, "colormap jet\n"); fclose(fp); printf("Matlab script written to %s\n", data->matlab_out); } #undef XVEC #undef TVEC #undef ZMAT void plot3d(struct plot3d *data) { if (data->geomview_out != NULL) plot_with_geomview(data); if (data->maple_out != NULL) plot_with_maple(data); if (data->matlab_out != NULL) plot_with_matlab(data); }