%% Voltage Divider
% This live script does two things. First, it calculates the voltage at a point between two resistors of a voltage divider. Second, it plots the measured voltage  for the given sets of resistors, fits a line to the measured values, determines the slope, and calculates the percent error in the slope as compared to the input voltage.
%
% Define five resistor values for each resistor in your circuit and define your input voltage.
%

R1 = [? ? ? ? ?];% 5 resistor values
R2 = [? ? ? ? ?];%5 resistor values
V = ?;%measured voltage of battery

%
%% Measurements
% Substitute your measured output voltage values.
%

V_meas = [? ? ? ? ?] %measurement output voltage values of your breadboard circuit

%
%% Calculations
% Call the function to calculate the output voltage, the percentage of the ouptut voltage to the input voltage, and the ratio of the last resistor to the equivalent resistance.
%

[V_out Percentage ratio] = voltDivider(?,?,?)
%hint - look at the function to find the input parameters to voltDivider()

%
%% Plot and Fit
% Plot the measured output voltage values as a function of the ratio of the second resistor to the equivalent resistance. Fit the data using polyfit and plot.
%

figure(1)
plot(?,?,'k*') % plot the ratio on the x-axis and V_meas on the y-axis
hold on
p=polyfit(ratio,V_meas,1) % finds the eqn for a fit to the line 
rat = 0:.01:1; % create a new set of x-values
y = polyval(p,rat) % find new y-values for your eqn
hold on;
plot(rat,y) % plot your new eqn that's a fit to your data
xlabel('$\frac{R_2}{R_1+R_2}$','Interpreter','latex')
ylabel('$V_{out}$','Interpreter','latex')

%
%% Compare
% Plot the calculate output voltage values as a function of the ratio of the second resistor to the equivalent resistance. Compare data points to make sure you're representing the resistors correctly in the circuit.
%

figure(2)
plot(?,?,'k*') % plot the ratio on the x-axis and V_out on the y-axis
xlabel('$\frac{R_2}{R_1+R_2}$','Interpreter','latex')
ylabel('$V_{out}$','Interpreter','latex')

%% Error
% Calculate the percent error of slope of the line (p(1)) as compared with the measured value of the battery voltage.
%

Error = 100*(?-p(1))/?

%

function [V_out Percentage ratio] = voltDivider(V,R1,R2)
    ratio = R2./(R1+R2);    
    V_out = V*ratio;    
    Percentage = 100*V_out/V;
end