Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- file_name = input('Enter the file name of data, must be in quotes and in the cwd \n');
- display(file_name)
- data = load(file_name);
- type = input('Select type of fit (1: linear, 2: polynomial, 3: exponential, 4: power) \n');
- if(type == 2)
- degree = input('Degree of polynomial: ');
- end
- %Call neccecary fit function based on user input
- if(type == 1)
- [result, gof] = get_linear_fit(data);
- plot_with_label(data, result, gof.rsquare);
- end
- if(type == 2)
- [result, gof] = get_poly_fit(data, degree);
- plot_with_label(data, result, gof.rsquare);
- end
- if(type == 3)
- [result, gof] = get_exp_fit(data);
- plot_with_label(data, result, gof.rsquare);
- end
- if(type == 4)
- [result, gof] = get_power_fit(data);
- plot_pow(data, result, gof.rsquare);
- end
- display(result)
- function [result, gof] = get_linear_fit(data)
- [result, gof] = get_poly_fit(data, 1);%call polynomial fit, but with degree 1
- end
- function [result, gof] = get_poly_fit(data, degree)
- x = data(:,1);%get x components
- y = data(:,2);%get y components
- polystring = strcat('poly', num2str(degree));%Set function type string(depends on degree)
- [result, gof] = fit(x,y, polystring);%fit data
- end
- function [result, gof] = get_exp_fit(data)
- x = data(:,1);%get x components
- y = data(:,2);%get y components
- [result, gof] = fit(x,y,'exp1');%fit data
- end
- function [result, gof] = get_power_fit(data)
- x = data(:,1);%get x components
- y = data(:,2);%get y components
- %Set fit options to allow for non zero start point
- FO = fitoptions('Method', 'NonlinearLeastSquares', 'StartPoint',[1 1 1]);
- FT = fittype('a*(x^b) + c', 'options', FO);
- [result, gof] = fit(x, y, FT);%fit data
- end
- %Plot method to be used if fit is anything but power
- function plot_with_label(data, fit, r)
- x = data(:,1);%get x components
- y = data(:,2);%get y components
- s = get_string(fit);%convert cfit type to string
- rs = strcat('r^2=', num2str(r));%Create string storing r^2 value
- plot(fit,x,y)%plot data
- legend("Raw Data",s)%draw legend
- text(min(x),max(y),rs)%Show r^2 in top left corner
- end
- %Plot method to be used if fit is power
- function plot_pow(data, fit, r)
- x = data(:,1);%get x components
- y = data(:,2);%get y components
- %generate string representation of function
- s = num2str(fit.a) + "* (x \^" + num2str(fit.b) + ") + " + num2str(fit.c);
- rs = strcat('r^2=', num2str(r));%Create string storing r^2 value
- plot(fit,x,y)%plot data
- legend("Raw Data",s)%draw legend
- text(min(x),max(y),rs)%Show r^2 in top left corner
- end
- function eq = get_string(fit)
- %The method below is from MATLAB support
- %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
- eq = formula(fit); %Formula of fitted equation
- parameters = coeffnames(fit); %All the parameter names
- values = coeffvalues(fit); %All the parameter values
- for idx = 1:numel(parameters)
- param = parameters{idx};
- l = length(param);
- loc = regexp(eq, param); %Location of the parameter within the string
- while ~isempty(loc)
- %Substitute parameter value
- eq = [eq(1:loc-1) num2str(values(idx)) eq(loc+l:end)];
- loc = regexp(eq, param);
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement