Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Clear all variables, close all windows, clear screen
- clear all; close all; clc;
- % Data points
- t = [0 5 10 15 20 25];
- b = [100 200 450 950 2000 5000];
- % Linearised data points
- x = t;
- y = log(b);
- % Calculates the line
- coefficients = polyfit(x,y,1);
- a1 = coefficients(1);
- a0 = coefficients(2);
- % Converts back to original curve
- alpha = exp(a0);
- beta = a1;
- % Function to plot
- range = @(domain) alpha.*exp(beta.*domain);
- % Extrapolated values
- extrapX = 30;
- extrapY = range(extrapX);
- % Plots the first figure according to the correct format
- plot(t,b,'kd')
- hold on
- plot(extrapX,extrapY,'ro');
- legend('Bacteria','Extrapolated data point','location','northwest');
- fprintf('At t = %f, extrapolated data point: %f',extrapX,extrapY);
- xlabel('Time (minutes)');
- ylabel('Bacteria');
- title('Bacteria vs Time')
- legend('Raw data','Extrapolated data point','location','northwest')
- fprintf('\na0: %f\na1: %f\n',a0,a1);
- % Sets the domain and order of polynimial with user input
- domain = linspace(0,30,60);
- n = input('\nEnter degree of polynomial: ');
- coefficientsp = polyfit(t,b,n);
- extrapYp = polyval(coefficientsp,extrapX);
- % Creates new figure
- % Plots the second polynomial figure
- figure();
- plot(domain,polyval(coefficientsp,domain));
- hold on
- plot(extrapX,extrapYp,'ro');
- hold on
- legend('Polynomial fit','Extrapolated data point','location','northwest');
- fprintf('At t = %f, extrapolated data point: %f\n',extrapX,extrapYp);
- xlabel('Time (minutes)');
- ylabel('Bacteria');
- title(sprintf('Bacteria vs Time (degree of %d)', n));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement