#include #include #include #include #include "struct_TAC_in.h" #include "struct_ROI.h" /* C source code name: fit_sv_akc.c */ /* edit, e.g., with emacs. compile with Makefile */ /* By: Alexander K. Converse, University of Wisconsin-Madison */ /* Created: 22 Nov 2012 */ /* Modified: 26 March 2014 */ /* Purpose: fit sv to ROI data using brute force minimization */ /* usage: fit_sv_akc(index, ROI struct, TAC struct, int MAT) */ void fit_sv_akc(int index, struct ROI *iROI, struct TAC_t *q, int MAT) { double difference; double smallestDifference; int found; int lastIndex; int i; double sv_start; double bestSV; double sv; double E; found = 0; // Define some epsilon the (observed - bound) calculation must be within E = .00005; // Set the starting, current, and prior sv to 0 sv_start = 0; sv = 0; //oldSV = 0; // Set the prior difference to arbitrarily large number smallestDifference = 1000; // Determine index of the last value in the array lastIndex = q->nFrames - 1; for (i = 0; i < 100; i++) { find_MAT_akc(index, 1, iROI, q, sv, MAT); difference = iROI->observed[lastIndex] - iROI->bound[lastIndex]; if (difference < 0) { difference = difference * -1; } //printf("Difference: %f SV: %f\n", difference, sv); if (difference < smallestDifference) { smallestDifference = difference; bestSV = sv; } sv = sv + 0.00001; } iROI->sv = bestSV; printf("Best sv: %f\n", iROI->sv); /* do { // Run model calculations for initial sv value. This gives starting observed and bound values // Goal is to minimize difference between observed and bound values at tfinal find_MAT_akc(index, 1, iROI, q, sv, MAT); // Calculate difference difference = iROI->observed[lastIndex] - iROI->bound[lastIndex]; // Make difference positive if (difference < 0) { difference = difference * -1; } // If difference is less than specified epsilon, store that value and exit by setting found = 1 if (difference < E) { iROI->sv = sv; found = 1; printf("Difference less than E\n"); } // If difference is greater than the previous difference, epsilon value was not reached // Store the previous difference as the smallest achievable difference and exit else if (difference >= oldDifference) { iROI->sv = oldSV; found = 1; printf("Difference > oldDifference, difference not < %f\n", E); } // If neither condition met, increase sv else { oldSV = sv; sv = sv + .00001; oldDifference = difference; found = 0; } } while (found == 0); */ }