How to assign outputs of a function to an anonymous function in order to optimize with lsqcurvefit
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Matt J
2024 年 3 月 22 日
編集済み: Matt J
2024 年 3 月 22 日
% 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 件のコメント
Matt J
2024 年 3 月 22 日
編集済み: Matt J
2024 年 3 月 22 日
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.
その他の回答 (1 件)
Torsten
2024 年 3 月 22 日
移動済み: Torsten
2024 年 3 月 22 日
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 件のコメント
Torsten
2024 年 3 月 22 日
編集済み: Torsten
2024 年 3 月 22 日
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] ?
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!