Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. % Clear all variables, close all windows, clear screen
  2. clear all; close all; clc;
  3.  
  4. % Data points
  5. t = [0 5 10 15 20 25];
  6. b = [100 200 450 950 2000 5000];
  7.  
  8. % Linearised data points
  9. x = t;
  10. y = log(b);
  11.  
  12. % Calculates the line
  13. coefficients = polyfit(x,y,1);
  14. a1 = coefficients(1);
  15. a0 = coefficients(2);
  16.  
  17. % Converts back to original curve
  18. alpha = exp(a0);
  19. beta = a1;
  20.  
  21. % Function to plot
  22. range = @(domain) alpha.*exp(beta.*domain);
  23.  
  24. % Extrapolated values
  25. extrapX = 30;
  26. extrapY = range(extrapX);
  27.  
  28. % Plots the first figure according to the correct format
  29. plot(t,b,'kd')
  30. hold on
  31. plot(extrapX,extrapY,'ro');
  32. legend('Bacteria','Extrapolated data point','location','northwest');
  33. fprintf('At t = %f, extrapolated data point: %f',extrapX,extrapY);
  34. xlabel('Time (minutes)');
  35. ylabel('Bacteria');
  36. title('Bacteria vs Time')
  37. legend('Raw data','Extrapolated data point','location','northwest')
  38. fprintf('\na0: %f\na1: %f\n',a0,a1);
  39.  
  40. % Sets the domain and order of polynimial with user input
  41. domain = linspace(0,30,60);
  42. n = input('\nEnter degree of polynomial: ');
  43. coefficientsp = polyfit(t,b,n);
  44. extrapYp = polyval(coefficientsp,extrapX);
  45.  
  46. % Creates new figure
  47. % Plots the second polynomial figure
  48. figure();
  49. plot(domain,polyval(coefficientsp,domain));
  50. hold on
  51. plot(extrapX,extrapYp,'ro');
  52. hold on
  53. legend('Polynomial fit','Extrapolated data point','location','northwest');
  54. fprintf('At t = %f, extrapolated data point: %f\n',extrapX,extrapYp);
  55. xlabel('Time (minutes)');
  56. ylabel('Bacteria');
  57. title(sprintf('Bacteria vs Time (degree of %d)', n));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement