calling exponential function to non linear square

I want to fit data to an exponential function with the nonlinear least squares method, but MATLAB gives an error.
The code that I've written :
clear;
clc;
close all;
%mix code: ref_concrete_T8
tdata = ...
[1.2560 2.7203 6.0830 10.0306 20.0960 40.9837 80.0923 ];
sdata = ...
[1.3000 7.2567 23.0167 30.2733 31.9400 43.0667 43.4850 ];
%fun=x1*exp(-(x2/tdata)^x3)
fun = @(x)x(1)*exp(-(x(2)./tdata)^x(3))-sdata;
x0=[40,0.9,0.1];
options = optimoptions(@lsqnonlin,'Algorithm','trust-region-reflective');
x = lsqnonlin(fun,x0)
plot(tdata,sdata,'ko')
hold on
tlist = linspace(tdata(1),tdata(end));
plot(tlist,x(1)*exp(-(x(2)./tdata)^x(3)),'b-')
xlabel time(day)
ylabel strength(MPa)
title('exponential Fit to Data ref conc T8')
legend('Data','exponential Fit')
hold off

2 comentarios

In your function you can't raise a matrix to a power. The only thing that is possible is to raise the power of each element in the matrix and for that the line should be
fun = @(x)x(1)*exp(-(x(2)./tdata).^x(3))-sdata;
Image Analyst
Image Analyst el 13 de Sept. de 2020
Can you put this down in the answer section Samiu? You can even get credit for it down there. Thanks in advance.

Iniciar sesión para comentar.

 Respuesta aceptada

Samiu Haque
Samiu Haque el 13 de Sept. de 2020
In your function you can't raise a matrix to a power. The only thing that is possible is to raise the power of each element in the matrix and for that the line should be
fun = @(x)x(1)*exp(-(x(2)./tdata).^x(3))-sdata;

5 comentarios

Zahra Safari
Zahra Safari el 13 de Sept. de 2020
THANKS A LOT.
i have one more question, how can i calculate R-squared of this method?
what is the code?
Zahra Safari
Zahra Safari el 13 de Sept. de 2020
another problem is that when matlab wants to plot the curvefitting, give bellow error:
Error using plot
Vectors must be the same lengths.
Error in asli (line 16)
plot(tlist,x(1)*exp(-(x(2)./tdata).^x(3)),'b-')
the code that i've written:
clc;
close all;
tdata = ...
[0.43083 0.87792 1.79667 2.59333 6.18667 24.68667 79.39000];
sdata = ...
[2.82 9.05 16.25 18.39 25.21 36.37 38.51];
fun = @(x)x(1)*exp(-(x(2)./tdata).^x(3))-sdata;
x0=[40,0.9,0.1];
options = optimoptions(@lsqnonlin,'Algorithm','trust-region-reflective');
[x,resnorm, residual] = lsqnonlin(fun,x0)
plot(tdata,sdata,'ko')
hold on
tlist = linspace(tdata(1),tdata(end));
plot(tlist,x(1)*exp(-(x(2)./tdata).^x(3)),'b-')
xlabel tdata
ylabel sdata
title('hyperbolic Fit to Data')
legend('Data','hyperbolic Fit')
hold off
Samiu Haque
Samiu Haque el 13 de Sept. de 2020
For plotting the graph, you need equal no. of x and y values. That means two data sets must be equal. But in your code, tlist contains 100 elements whereas your function produces only 7 elements. For that reason, it is showing the error 'Vectors must be the same lengths'.
Use the following portion to solve the problem
syms tlist
y = x(1)*exp(-(x(2)./tlist).^x(3));
tlist = linspace(tdata(1),tdata(end));
y = subs(y);
plot(tlist,y,'b-')
Zahra Safari
Zahra Safari el 14 de Sept. de 2020
thank you.
how can i calculate the value of R-squared??

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 13 de Sept. de 2020

Comentada:

el 14 de Sept. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by