Advertisement
Guest User

A4.m

a guest
Nov 12th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.38 KB | None | 0 0
  1. file_name = input('Enter the file name of data, must be in quotes and in the cwd \n');
  2. display(file_name)
  3. data = load(file_name);
  4.  
  5. type = input('Select type of fit (1: linear, 2: polynomial, 3: exponential, 4: power) \n');
  6. if(type == 2)
  7.     degree = input('Degree of polynomial: ');
  8. end
  9.  
  10. %Call neccecary fit function based on user input
  11. if(type == 1)
  12.     [result, gof] = get_linear_fit(data);
  13.     plot_with_label(data, result, gof.rsquare);
  14. end
  15. if(type == 2)
  16.     [result, gof] = get_poly_fit(data, degree);
  17.     plot_with_label(data, result, gof.rsquare);
  18. end
  19. if(type == 3)
  20.     [result, gof] = get_exp_fit(data);
  21.     plot_with_label(data, result, gof.rsquare);
  22. end
  23. if(type == 4)
  24.     [result, gof] = get_power_fit(data);
  25.     plot_pow(data, result, gof.rsquare);
  26. end
  27.  
  28. display(result)
  29.  
  30. function [result, gof] = get_linear_fit(data)
  31.     [result, gof] = get_poly_fit(data, 1);%call polynomial fit, but with degree 1
  32. end
  33. function [result, gof] = get_poly_fit(data, degree)
  34.     x = data(:,1);%get x components
  35.     y = data(:,2);%get y components
  36.     polystring = strcat('poly', num2str(degree));%Set function type string(depends on degree)
  37.     [result, gof] = fit(x,y, polystring);%fit data
  38. end
  39.  
  40. function [result, gof] = get_exp_fit(data)
  41.     x = data(:,1);%get x components
  42.     y = data(:,2);%get y components
  43.     [result, gof] = fit(x,y,'exp1');%fit data
  44. end
  45.  
  46. function [result, gof] = get_power_fit(data)
  47.     x = data(:,1);%get x components
  48.     y = data(:,2);%get y components
  49.     %Set fit options to allow for non zero start point
  50.     FO = fitoptions('Method', 'NonlinearLeastSquares', 'StartPoint',[1 1 1]);
  51.     FT = fittype('a*(x^b) + c', 'options', FO);
  52.     [result, gof] = fit(x, y, FT);%fit data
  53. end
  54.  
  55.  
  56. %Plot method to be used if fit is anything but power
  57. function plot_with_label(data, fit, r)
  58.     x = data(:,1);%get x components
  59.     y = data(:,2);%get y components
  60.     s = get_string(fit);%convert cfit type to string
  61.     rs = strcat('r^2=', num2str(r));%Create string storing r^2 value
  62.     plot(fit,x,y)%plot data
  63.     legend("Raw Data",s)%draw legend
  64.     text(min(x),max(y),rs)%Show r^2 in top left corner
  65.  
  66. end
  67.  
  68. %Plot method to be used if fit is power
  69. function plot_pow(data, fit, r)
  70.     x = data(:,1);%get x components
  71.     y = data(:,2);%get y components
  72.     %generate string representation of function
  73.     s = num2str(fit.a) + "* (x \^" + num2str(fit.b) + ") + " + num2str(fit.c);  
  74.     rs = strcat('r^2=', num2str(r));%Create string storing r^2 value
  75.     plot(fit,x,y)%plot data
  76.     legend("Raw Data",s)%draw legend
  77.     text(min(x),max(y),rs)%Show r^2 in top left corner
  78.  
  79. end
  80.  
  81.  
  82. function eq = get_string(fit)
  83.     %The method below is from MATLAB support
  84.     %https://www.mathworks.com/matlabcentral/answers/99155-how-do-i-extract-the-output-equation-from-a-cfit-object-in-curve-fitting-toolbox-2-0-r2009a
  85.     eq = formula(fit); %Formula of fitted equation
  86.     parameters = coeffnames(fit); %All the parameter names
  87.     values = coeffvalues(fit); %All the parameter values
  88.     for idx = 1:numel(parameters)
  89.           param = parameters{idx};
  90.           l = length(param);
  91.           loc = regexp(eq, param); %Location of the parameter within the string
  92.           while ~isempty(loc)    
  93.               %Substitute parameter value
  94.               eq = [eq(1:loc-1) num2str(values(idx)) eq(loc+l:end)];
  95.               loc = regexp(eq, param);
  96.           end
  97.     end
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement