How to assign outputs of a function to an anonymous function in order to optimize with lsqcurvefit
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Matt J
on 22 Mar 2024
Edited: Matt J
on 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 Comments
Matt J
on 22 Mar 2024
Edited: Matt J
on 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.
More Answers (1)
Torsten
on 22 Mar 2024
Moved: Torsten
on 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)
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 Comments
Torsten
on 22 Mar 2024
Edited: Torsten
on 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] ?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!