#include #include "neural-nets-ode.h" // The name of the array of coords in the Matlab script #define NN "NN_plot_with_matlab" /* Writes a Matlab script to plot the solution sampled at n+1 equally * spaced points x[0] = a, x[1], x[2], ..., x[n] on the interval [a,b]. * The distance between a pair consecutive points is (b-a)/n. * * If the script is named file.m, then execute it in Matlab by * entering the command * run('/path/to/file.m') * or, if the file is located in the current directory, simply say * file */ void Neural_Net_plot_with_matlab(struct Neural_Net_ODE *nn, int n, char *outfile) { double a = nn->a; double b = nn->b; FILE *fp; if ((fp = fopen(outfile, "w")) == NULL) { fprintf(stderr, "unable to open file `%s' for writing\n", outfile); return; } fprintf(fp, "close all\n"); fprintf(fp, "%s = [ ", NN); for (int i = 0; i <= n; i++) { double x = a + (b-a)/n*i; Neural_Net_phi(nn, x); Neural_Net_eval(nn, x); double y = nn->N[0] * nn->phi[0]; fprintf(fp, "%g, %g; ", x, y); } fprintf(fp, "];\n"); fprintf(fp, "plot(%s(:,1), %s(:,2), 'r');\n", NN, NN); fprintf(fp, "title({'hidden units = %d, training points = %d'", nn->q, nn->nu); if (nn->exact_sol != NULL) fprintf(fp, ", 'error vs exact = %g'", Neural_Net_error_vs_exact(nn, n)); fprintf(fp, "});\n"); fprintf(fp, "grid on;\n"); fclose(fp); } #undef NN