Encountering error with using lsqcurvefit
6 次查看(过去 30 天)
显示 更早的评论
I have the following experimental data.
xdata = [1 1.01 1.12 1.24 1.39 1.61 1.89 2.17 2.42 3.01 3.58 4.03 4.76 5.36 5.76 6.16 6.40 6.62 6.87 7.05 7.16 7.27 7.43 7.50 7.61];
ydata = [0 0.03 0.14 0.23 0.32 0.41 0.50 0.58 0.67 0.85 1.04 1.21 1.58 1.94 2.29 2.67 3.02 3.39 3.75 4.12 4.47 4.85 5.21 5.57 6.30];
I am trying to fit the ydata, which are stresses denoted by P, to the analytical equivalent (see equation) where the xdata represents the corresponding strains denoted by λ.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/482423/image.png)
I am attempting to use the lsqcurvefit function in the follwing script but I am encountering an error. How may I resolve this? I am unfamiliar with most of the information returned to me
clear all
close all
clc
% xdata (Stretch, Lamda)
xdata = [1 1.01 1.12 1.24 1.39 1.61 1.89 2.17 2.42 3.01 3.58 4.03 4.76 5.36 5.76 6.16 6.40 6.62 6.87 7.05 7.16 7.27 7.43 7.50 7.61];
% ydata (Nominal Stress, P)
ydata = [0 0.03 0.14 0.23 0.32 0.41 0.50 0.58 0.67 0.85 1.04 1.21 1.58 1.94 2.29 2.67 3.02 3.39 3.75 4.12 4.47 4.85 5.21 5.57 6.30];
% Proposition
% ydata = (a*b*xdata^3 - 1))/(xdata)*(xdata*b - xdata^3 + 3*xdata - 2));
% Gent Model Y-Axis
gent_function = @(x,xdata)(x(1)*x(2)*(xdata^3 - 1))/((xdata)*(xdata*x(2) - xdata^3 + 3*xdata - 2));
% Fit Model w/ Start
x0 = [100, 0.1];
x = lsqcurvefit(gent_function,x0,xdata,ydata);
% Plot Data & Fitted Curve
times = linspace(xdata(1),xdata(end));
plot(xdata,ydata,'ko',times,gent_function(x,times),'b-')
legend('Data','Fitted Exponential')
title('Data and Fitted Curve')
--------------------------------------------------------------------------------------------------
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a
power. Check that the matrix is square and the power
is a scalar. To perform elementwise matrix powers,
use '.^'.
Error in
check>@(x,xdata)(x(1)*x(2)*(xdata^3-1))/((xdata)*(xdata*x(2)-xdata^3+3*xdata-2))
(line 15)
gent_function = @(x,xdata)(x(1)*x(2)*(xdata^3 -
1))/((xdata)*(xdata*x(2) - xdata^3 + 3*xdata - 2));
Error in lsqcurvefit (line 225)
initVals.F =
feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in check (line 19)
x = lsqcurvefit(gent_function,x0,xdata,ydata);
Caused by:
Failure in initial objective function
evaluation. LSQCURVEFIT cannot continue.
0 个评论
采纳的回答
Walter Roberson
2021-1-8
% xdata (Stretch, Lamda)
xdata = [1 1.01 1.12 1.24 1.39 1.61 1.89 2.17 2.42 3.01 3.58 4.03 4.76 5.36 5.76 6.16 6.40 6.62 6.87 7.05 7.16 7.27 7.43 7.50 7.61];
% ydata (Nominal Stress, P)
ydata = [0 0.03 0.14 0.23 0.32 0.41 0.50 0.58 0.67 0.85 1.04 1.21 1.58 1.94 2.29 2.67 3.02 3.39 3.75 4.12 4.47 4.85 5.21 5.57 6.30];
% Proposition
% ydata = (a*b*xdata^3 - 1))/(xdata)*(xdata*b - xdata^3 + 3*xdata - 2));
% Gent Model Y-Axis
gent_function = @(x,xdata)(x(1)*x(2)*(xdata.^3 - 1))./((xdata).*(xdata*x(2) - xdata.^3 + 3*xdata - 2));
% Fit Model w/ Start
x0 = [100, 0.1];
x = lsqcurvefit(gent_function,x0,xdata,ydata);
% Plot Data & Fitted Curve
times = linspace(xdata(1),xdata(end));
plot(xdata,ydata,'ko',times,gent_function(x,times),'b-')
legend('Data','Fitted Exponential')
title('Data and Fitted Curve')
3 个评论
Walter Roberson
2021-1-8
% xdata (Stretch, Lamda)
xdata = [1 1.01 1.12 1.24 1.39 1.61 1.89 2.17 2.42 3.01 3.58 4.03 4.76 5.36 5.76 6.16 6.40 6.62 6.87 7.05 7.16 7.27 7.43 7.50 7.61];
% ydata (Nominal Stress, P)
ydata = [0 0.03 0.14 0.23 0.32 0.41 0.50 0.58 0.67 0.85 1.04 1.21 1.58 1.94 2.29 2.67 3.02 3.39 3.75 4.12 4.47 4.85 5.21 5.57 6.30];
% Proposition
% ydata = (a*b*xdata^3 - 1))/(xdata)*(xdata*b - xdata^3 + 3*xdata - 2));
% Gent Model Y-Axis
gent_function = @(x,xdata)(x(1)*x(2)*(xdata.^3 - 1))./((xdata).*(xdata*x(2) - xdata.^3 + 3*xdata - 2));
residue_function = @(x) sum(((x(1)*x(2)*(xdata.^3 - 1))./((xdata).*(xdata*x(2) - xdata.^3 + 3*xdata - 2)) - ydata).^2);
% Fit Model w/ Start
x0 = [100, 0.1];
gs = GlobalSearch;
problem = createOptimProblem('fmincon', 'x0', x0, ...
'objective', residue_function);
x = run(gs,problem)
% Plot Data & Fitted Curve
times = linspace(xdata(1),xdata(end));
plot(xdata,ydata,'ko',times,gent_function(x,times),'b-')
legend('Data','Fitted Exponential')
title('Data and Fitted Curve')
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!