How to assign outputs of a function to an anonymous function in order to optimize with lsqcurvefit

4 visualizzazioni (ultimi 30 giorni)
I'm willing to preform an optimization process in order to fit the simulation result to test data and find some material properties.
For example I have a function which performs simulation (in ABAQUS for my case) and gives out a set of x-y data like this:
function [u_sim,f_sim] = myfun(x)
u_sim = (0.9:8.05:81.3);
f_sim = x(1).*exp(x(2).*u_sim);
end
Then I want to fit this result to test data by best choise of constants a(1) and a(2) with lsqcurvefit like this:
clear all;
% test data
xdata = ...
[0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
ydata = ...
[455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];
% optimization of constants a(:)
x0 = [100,-1];
fun1 = @(x,u_sim) myfun(x); % ?????
x = lsqcurvefit(fun1,x0,xdata,ydata); % ?????
But my problem is that I don't know how to adjust fun1 with lsqcurvefit to make optimization process work. Because fun1 needs to have the same x values as test data (like xdata in this example) as far as I've understood. In other words, I want to fit a series of x-y data to another, meaning that (u_sim,f_sim) should be fitted to (xdata,ydata).
Can anyone help me with this?
Thanks in advance.

Risposta accettata

Matt J
Matt J il 22 Mar 2024
Modificato: Matt J il 22 Mar 2024
% test data
xdata = ...
[0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
ydata = ...
[455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];
% optimization of constants a(:)
a0 = [100,-1];
fun1 = @(a,u_sim) myfun(a,u_sim);
x = lsqcurvefit(fun1,a0,xdata,ydata);
function f_sim = myfun(a,u_sim)
f_sim = a(1).*exp(a(2).*u_sim);
end
  6 Commenti
Matt J
Matt J il 22 Mar 2024
Modificato: Matt J il 22 Mar 2024
So you mean x-values for fitting data and fitting function should necessarily be the same?
I don't know what the difference is, in your mind, between "fitting data" and "fitting function". To me, they sound like the same thing.
The bottom line though is that myfun needs to be able to accept arbitrary x-axis values as input. This makes it so that lsqcurvefit can give myfun one set of x-axis values for the purposes of performing the fit (xdata), but also so that you can use a possibly different set of x-axis values for other purposes later. For example, you might want to plot the fit at locations other than those in xdata.
Esi Tesi
Esi Tesi il 22 Mar 2024
So looks like we can conclude as you said:
The bottom line though is that myfun needs to be able to accept arbitrary x-axis values as input.
But now I think you were right about the bug existing in myfun and I'm trying to fix it. I just realized that I can obtain f_sim for values of xdata using a trick. Actually you gave me the idea.
Thank you for your time.

Accedi per commentare.

Più risposte (1)

Torsten
Torsten il 22 Mar 2024
Spostato: Torsten il 22 Mar 2024
What hinders you from using the usual coding like this ?
% test data
xdata = ...
[0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
ydata = ...
[455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];
x0 = [100,-1];
x = lsqcurvefit(@myfun,x0,xdata,ydata)
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
x = 1x2
498.8309 -0.1013
hold on
plot(xdata,ydata,'o')
plot(xdata,myfun(x,xdata))
hold off
grid on
function f = myfun(x,xdata)
f = x(1).*exp(x(2).*xdata);
end
  3 Commenti
Torsten
Torsten il 22 Mar 2024
Modificato: Torsten il 22 Mar 2024
So I have (u_sim,f_sim) which should be fitted to (xdata,ydata).
Could you explain this more clearly ?
Usually, you have an independent variable x and a dependent variable y and a function f for which you think it is appropriate to connect x and y: y = f(p,x) with certain free parameters p.
For certain x values, you have measurement data xdata and associated ydata. Now you try to adjust p such that f(p*,xdata) - ydata becomes small.
What is different in your case ? What is the difference between usim and xdata ? Maybe you want t use them together as XDATA = [usim,xdata] and YDATA = [fsim,ydata] ?
Esi Tesi
Esi Tesi il 22 Mar 2024
Modificato: Esi Tesi il 22 Mar 2024
xdata are the x-values that I have previously obtained from experimental tests but u_sim are the x-values that I get from some simulation in another software (inside myfun function) and should be calculated inside myfun as well as f_sim.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by