function tot = tot_sq_err(x) % Calculate total squared error between diode model and % measured data. % % Requires globally defined arrays: % v_diode = array of measured voltages for data points % i_diode = array of measured currents for data points % Example: % If the data pts as ordered pairs of (voltage, current) were: % (0.0, 0.13), (0.1, 0.24), (0.2, 0.37). % The corresponding arrays would be: % v_diode = [0.0, 0.1, 0.2]; % i_diode = [0.13, 0.24, 0.37]; global v_diode global i_diode % Extract Is and T from array x that is function argument. Is = x(1); T = x(2); % Define physical constants. k = 1.38e-23; % J/degK Boltzmann constant q = 1.602e-19; % C electron charge % Clear the total squared error variable. tot = 0; % Add up the squared errors for all the data points. for ind = 1 : length(v_diode) % Calculate the diode current from the model. i_model = Is * (exp(v_diode(ind) / (k*T/q)) - 1); % Square the error in the model prediction for this data point. i_err_sq = (i_diode(ind) - i_model)^2; % Add the squared error to the running sum. tot = tot + i_err_sq; end return