Displaying different strings on a graph based on an input of a function handle

1 visualización (últimos 30 días)
Disclaimer: This is for a homework assignment so do not just solve it for me persay, just help me understand how I would do this :)
So I need to create a bunch of functions to find a best fit line at different degree and with different parameters(ex: linear, constant, exponential, quadratic ect). The output is a function handle of the best fit line found using polyfit on the data file that was input. Here is an example for linear:
function [bf_linear] = bestFit_linear(fileName)
xy = load(fileName);
x = xy(:,1);
y = xy(:,2);
params = polyfit(x, y, 1);
bf_linear = @(xvar) polyval(params, xvar);
end
I do not need help with these as I know how to do them all, however I need to create function that will plot the data and that best fit line and it needs to be able to do all of different best fit function. What I need help with is how do I make the code display a string of text on the graph that changes based on the function handle that is input(describing the input function on the graph basically). I thought I would use conditional statement to display it based on the function handle input but it said you cannot use a function handle. Here is the plotting function but it's pretty basic and works, I just need the string:
function [] = plotData_bestFit(dataFile, bestFitFunction) %bestFitFunction is the function of a specific type of best fit line with the same
xy = load(dataFile); %data file inside
plot(xy(:,1), xy(:,2), 'rx');
hold ON
fplot(bestFitFunction, 'k-')
title('Data'); xlabel('X values'); ylabel('Y Values');
daspect = [1 1 1];
axis([min(xy(:,1))-1 max(xy(:,1))+1 min(xy(:,2))-1 max(xy(:,2))+1]);
hold OFF
end
If anyone could point me in the right direction that would be great! Thanks!

Respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 26 de Mzo. de 2021
If I understood your question correctly, you're trying to display your found best fit model on the plot surface, correct?
If this is the case, you'd need to embed in your code the coefficient of determination (R^2 or adjusted R^2 is more preferable in your case since you are comparing several polynomials) to find out which fit model gives you the best fit. Then based on the ranking of your computed coefficient of determination values, your code chooses to display the best fit model using this way, for instance:
% If the linear fit is the best fit, then its formulation is diplayed:
gtext(['Fit model is: ' num2str(a) '*x + (' num2str(b) ')' ])
Good luck.

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by