error trying to fit two arrays with an exponential fit

2 visualizzazioni (ultimi 30 giorni)
I am trying to fit to arrays and I get the error 'Check for missing argument or incorrect argument data type in call to function 'fit''.
The code I use:
clear all
close all
clc
year=[1991,1994,1995,1996,1997,2000,2001:1:2004,2007,2008,2016,2021,2024]
capacity=[ 0.4,0.5,0.5,0.7,0.6,2,2,2,3,4.5,5,5,10,12,14]
matrix=[year;capacity]
f=fit(year,capacity,'exp1')

Risposte (3)

Walter Roberson
Walter Roberson il 7 Set 2020
f=fit(year.',capacity.','exp1')
You have to pass in columns, but you were passing in rows
  3 Commenti
Walter Roberson
Walter Roberson il 7 Set 2020
It is not dots, it is dot apostrophe, which is the transpose operator.
year=[1991,1994,1995,1996,1997,2000,2001:1:2004,2007,2008,2016,2021,2024]
capacity=[ 0.4,0.5,0.5,0.7,0.6,2,2,2,3,4.5,5,5,10,12,14]
f=fit(year.',capacity.','exp1')
year =
Columns 1 through 14
1991 1994 1995 1996 1997 2000 2001 2002 2003 2004 2007 2008 2016 2021
Column 15
2024
capacity =
Columns 1 through 8
0.400000000000000 0.500000000000000 0.500000000000000 0.700000000000000 0.600000000000000 2.000000000000000 2.000000000000000 2.000000000000000
Columns 9 through 15
3.000000000000000 4.500000000000000 5.000000000000000 5.000000000000000 10.000000000000000 12.000000000000000 14.000000000000000
f =
General model Exp1:
f(x) = a*exp(b*x)
Coefficients (with 95% confidence bounds):
a = 1.035e-67 (-2.646e-66, 2.853e-66)
b = 0.07754 (0.06438, 0.0907)
Laura Stokbro
Laura Stokbro il 11 Set 2020
If I write f=fit(year.',capacity.','exp1')
I get the same error :(

Accedi per commentare.


Image Analyst
Image Analyst il 7 Set 2020
Modificato: Image Analyst il 8 Set 2020
Try this:
% Uses fitnlm() to fit a non-linear model (an exponential decay curve) through noisy data.
% Requires the Statistics and Machine Learning Toolbox, which is where fitnlm() is contained.
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
year=[1991,1994,1995,1996,1997,2000,2001:1:2004,2007,2008,2016,2021,2024]
capacity=[ 0.4,0.5,0.5,0.7,0.6,2,2,2,3,4.5,5,5,10,12,14]
% Create the X coordinates. Subtract 1991 so it's not so huge.
X = year(:) - min(year);
Y = capacity(:); % Get a column vector.
plot(year, capacity, 'b.-', 'LineWidth', 2);
grid on;
% Now we have noisy training data that we can send to fitnlm().
% Plot the noisy initial data.
plot(X, Y, 'b*', 'LineWidth', 2, 'MarkerSize', 15);
grid on;
% Convert X and Y into a table, which is the form fitnlm() likes the input data to be in.
tbl = table(X(:), Y(:));
% Define the model as Y = a + exp(-b*x)
% Note how this "x" of modelfun is related to big X and big Y.
% x((:, 1) is actually X and x(:, 2) is actually Y - the first and second columns of the table.
modelfun = @(b,x) b(1) * exp(b(2)*x(:, 1)) + b(3);
beta0 = [7, .05, -8]; % Guess values to start with. Just make your best guess.
% Now the next line is where the actual model computation is done.
mdl = fitnlm(tbl, modelfun, beta0);
% Now the model creation is done and the coefficients have been determined.
% YAY!!!!
% Extract the coefficient values from the the model object.
% The actual coefficients are in the "Estimate" column of the "Coefficients" table that's part of the mode.
coefficients = mdl.Coefficients{:, 'Estimate'}
% Create smoothed/regressed data using the model:
yFitted = coefficients(1) * exp(coefficients(2)*X) + coefficients(3);
% Now we're done and we can plot the smooth model as a red line going through the noisy blue markers.
hold on;
plot(X, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('Exponential Regression with fitnlm()', 'FontSize', fontSize);
xlabel('X - 1991', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
legendHandle = legend('Noisy Y', 'Fitted Y', 'Location', 'north');
legendHandle.FontSize = 30;
formulaString = sprintf('Y = %.3f * exp(%.3f * X) + %.3f', coefficients(1), coefficients(2), coefficients(3))
text(5, 10, formulaString, 'FontSize', 25, 'FontWeight', 'bold');
% Set up figure properties:
% Enlarge figure to full screen.
g = gcf;
g.WindowState = 'maximized';
% Give a name to the title bar.
set(g, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
fprintf('Done running %s.m ...\n', mfilename);

Laura Stokbro
Laura Stokbro il 11 Set 2020
Thank you so much! That helped a lot!
Is there a way I can retrieve R^2 using this code?

Categorie

Scopri di più su Descriptive Statistics in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by