How to fit an exponential curve? error: Dimensions of arrays being concatenated are not consistent

3 views (last 30 days)
Hello,
I have an energy plot that I would like to fit an exponential curve to.
I have something like this
numSteps = 5000;
x = 0:1:100;
time = zeros(1,numSteps+1);
ex_data = zeros(length(x),numSteps+1);
ex_data(:,1) = ex;
E_f = zeros(1, numSteps+1);
E_f(1) = 0.5*sum(ex.^2);
for i = 1:numSteps
%some calculations
[ex] = func(..) % ex calculated through some function
time(i+1) = (i)*dt;
E_f(i+1) = 0.5*sum(ex.^2 ) ;
end
%plot
figure(1)
s = fittype('a-b*exp(-c*x)');
f = fit(time, E_f, s, 'StartPoint', [[ones(size(time)), -exp(-time)]\E_f; 1]);
xx = linspace(0,50,250);
plot(time, E_f, 'o', xx, f(xx), 'r-');
grid on
title('Electric Field Energy')
I am trying to fit an exponential to this plot from xlim([ 0 80]) but I am getting the error
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in WeakBeam (line 184)
f = fit(time, E_f, s, 'StartPoint', [[ones(size(time)), -exp(-time)]\E_f; 1]);
I will post the entire code with functions if needed, thank you.

Answers (1)

Matt J
Matt J on 3 Mar 2021
Edited: Matt J on 3 Mar 2021
Did you mean,
[[ones(size(time)); -exp(-time)].\E_f; ones(size(time))]
  2 Comments
Jamie Al
Jamie Al on 3 Mar 2021
Hmm I am getting this error now:
Error using fit>iFit (line 135)
X must be a matrix with one or two columns.
Error in fit (line 116)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in WeakBeam (line 223)
f = fit(time, E_f, s, 'StartPoint',[[ones(size(time)); -exp(-time)].\E_f; ones(size(time))]);
Matt J
Matt J on 3 Mar 2021
Edited: Matt J on 3 Mar 2021
time , E_f, s must all be column vectors. It looks like you made them row vectors. Also, make sure that that complicated looking calculation of your StartPoint really results in a vector of 3 parameters.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!