現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
lscurve fit problem issue in Money Rivlin model
9 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I have got a function with respect to different variables. I want to make sum of that function with respect to the variables. my equations are
fun1=@(a,x1) a(1).*(2*x1-2./x1.^2)+a(2).*(2-2./x1.^3)+a(3).*(6* x1.^2-6*x1-6./x1.^4+6./x1.^3 +6./x1.^2-6)
and
fun2=@(a,x2) a(1).*(2*x2)+a(2).*(2*x2)+a(3).*(4*x2.^3)
I want to add fun1 and fun2
fun_sum= @(a,x1,x2) fun(a,x1)+ fun(a,x2)
now by applying lscurvefit I want values of a(1), a(2) and a(3)
I am looking forward for reply
1 件のコメント
採用された回答
Star Strider
2023 年 2 月 20 日
I am not certain what you want to do.
Two independent variables may require one or two dependent variables, however with only one dependent variable, it will need to be duplicated to two columns (assuming all data are column-oriented) —
fun1=@(a,x1) a(1).*(2*x1-2./x1.^2)+a(2).*(2-2./x1.^3)+a(3).*(6* x1.^2-6*x1-6./x1.^4+6./x1.^3 +6./x1.^2-6);
fun2=@(a,x2) a(1).*(2*x2)+a(2).*(2*x2)+a(3).*(4*x2.^3);
fun12 = @(a,x1x2) [fun1(a,x1x2(:,1)) fun2(a,x1x2(:,2))];
x1x2 = rand(12,2);
y1y2 = rand(12,2);
B0 = rand(3,1);
B = lsqcurvefit(fun12, B0, x1x2, y1y2) % Two Dependent Variables
Local minimum found.
Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
B = 3×1
-0.0744
0.0176
-0.0004
y1y1 = [1 1].*y1y2(:,1); % Duplicate First Column
B = lsqcurvefit(fun12, B0, x1x2, y1y1) % One Dependent Variable, Duplicated
Local minimum found.
Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
B = 3×1
-0.0865
0.0210
-0.0004
.
20 件のコメント
SATYA PAL
2023 年 2 月 20 日
Dear sir
I have to different functions of different variables and after summing them I want to apply lscurvefit to find out the constants. First function contaions compression test data( comp stress, comp strain i.e. fun1=@(a,x1) a(1).*(2*x1-2./x1.^2)+a(2).*(2-2./x1.^3)+a(3).*(6* x1.^2-6*x1-6./x1.^4+6./x1.^3 +6./x1.^2-6)) and second contains shear test data (shear stress and shear strain i.e.( fun2=@(a,x2) a(1).*(2*x2)+a(2).*(2*x2)+a(3).*(4*x2.^3))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1300990/image.png)
Star Strider
2023 年 2 月 20 日
I am not certain that you can sum them (amd I am not certain what that means) if they have different numbers of elements. The approach that I use here estimates the parameters for the two functions with the same data and same parameters.
One possibility —
fun1=@(a,x1) a(1).*(2*x1-2./x1.^2)+a(2).*(2-2./x1.^3)+a(3).*(6* x1.^2-6*x1-6./x1.^4+6./x1.^3 +6./x1.^2-6);
fun2=@(a,x2) a(1).*(2*x2)+a(2).*(2*x2)+a(3).*(4*x2.^3);
fun12 = @(a,x1x2) sum([fun1(a,x1x2(:,1)) fun2(a,x1x2(:,2))].^2,2); % Sum Squares Of Functions
x1x2 = rand(12,2);
y1y1 = rand(12,1);
B0 = rand(3,1);
B = lsqcurvefit(fun12, B0, x1x2, y1y1) % Two Dependent Variables
Local minimum possible.
lsqcurvefit stopped because the size of the current step is less than
the value of the step size tolerance.
B = 3×1
0.0157
-0.0014
0.0000
This computes ‘fun12’ as ‘fun1^2 + fun2^2’. There may be other ways to square them and sum them as well, depending on what you want to do. The order of summing and squaring (summing then squaring or squaring then summing) is important, and produce different results. The summed-and-squared functions, regardless of the order, produce a one-column result, so that requires a single dependent variable to fit.
.
SATYA PAL
2023 年 2 月 20 日
Dear sir
Thanks for help. what if we have different number of inputs in fun1 and fun2. with different number of inputs it's showing "Arrays have incompatible sizes for this operation"
Star Strider
2023 年 2 月 20 日
The functions have to return the same size vectors if you are to work with them as I described earlier.
One option would be to vertically concatenate them instead of horizontally concatenating them —
fun1=@(a,x1) a(1).*(2*x1-2./x1.^2)+a(2).*(2-2./x1.^3)+a(3).*(6* x1.^2-6*x1-6./x1.^4+6./x1.^3 +6./x1.^2-6);
fun2=@(a,x2) a(1).*(2*x2)+a(2).*(2*x2)+a(3).*(4*x2.^3);
% fun12 = @(a,x1x2) sum([fun1(a,x1x2(:,1)); fun2(a,x1x2(:,2))].^2,2); % Sum Squares Of Functions
fun12 = @(a,x) [fun1(a,x(1:6)); fun2(a,x(7:15))];
x1 = rand(6,1);
x2 = rand(9,1);
y1 = rand(6,1);
y2 = rand(9,1);
x1x2 = [x1; x2];
y1y2 = [y1; y2];
B0 = rand(3,1);
B = lsqcurvefit(fun12, B0, x1x2, y1y2) % Two Dependent Variables
Local minimum found.
Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
B = 3×1
1.3650
-1.0124
0.0700
The problem with this is that summing and squaring ceases to make sense in this context (since summing and squaring a vector returns a scalar, and that will not work with regressing vector independent and dependent variables). In order to do that, they should have the same sizes, and use the earlier description of ‘fun12’.
The only other option is to truncate the longer independent and dependent vectors to the length of the shorter vectors, and then use the earlier implementation that uses equal-length vectors. (How you create the shortened vectors is entirely up to you.) This is likely the best approach if adding and squaring continue to be required.
The alternative of adding (concatenating) elements of the shorter vectors to equal the lengths of the longer vectors has the undesirable effect of ‘weighting’ the duplicated elements disproportionately, resulting in estimated parameters that do not reflect the actual data.
.
SATYA PAL
2023 年 2 月 20 日
Dear sir
can we solve the problem with '
"operation with incompatible arrays sizes"
Star Strider
2023 年 2 月 20 日
I described two ways of dealing with that in my previous Comment.
If you want to sum and square the two function results, the independent and dependent array sizes have to be equal. There is simply no other way to deal with that. I have no idea what your data are, or what they represent, so I suggest that in the longer vector, the independent variables be chosen to span approximately the same minimum and maximum values, and the corresponding dependent variables be truncated accordingly. (This will require removing the same rows from both vectors in the longer array so that it matches the length of the shorter array.) I see no other way to deal with the row length discrepancy and still do what you want. If you did not want to sum and square the function results, then vertically concatenating them would work.
With the summing and squaring requirement, the only option that I believe would be compatible would be to shorten the longer array.
SATYA PAL
2023 年 2 月 21 日
Thanks for help sir. I got my results.
one more thing I want to ask if any graph plotted with some data point (say 100). now we can get 500 data points from this graph directly from any code without any digitize (click and get data tool)
SATYA PAL
2023 年 2 月 21 日
h=openfig('abc.fig');
>> h=findobj(gca,'Type','line');
>> x=get(h,'Xdata');
>> y=get(h,'Ydata');
>> a=[];
>> a(:,1)=x;
>> a(:,2)=y;
>> dlmwrite('data.txt',a,',');
I am using this code but giving me fix data and i want more data at fix step
Star Strider
2023 年 2 月 21 日
I do not have ‘abc.fig’ however (depending on the MATLAB release/version you are using) that should work, if there is only one line object in the figure.
I do not understand what you mean by ‘fix data’ or ‘fix step’.
Also, it may be necessary to code these as:
x=get(h,'XData');
y=get(h,'YData');
Note the difference.
Attaching ‘abc.fig’ would help.
.
SATYA PAL
2023 年 2 月 21 日
Sir my question is I plotted a graph with 180 data and I want to extract 500 data points from the same graph.
Star Strider
2023 年 2 月 21 日
I do not recommend doing it because this creates data where none previously existed. You have no idea what the process that created your data actuallly did in those other 320 instances.
SATYA PAL
2023 年 2 月 22 日
Dear Sir
I've 180 exp data and generted 320 data with help of interpolation. Now I want to merge both to make it 500 data points. help me out.
Star Strider
2023 年 2 月 22 日
I cannot see what you are doing, so I am doing my best to imagine it.
Use the interpolated data vectors (or matrices) as you calculated them. They should have column lengths of 500 elements. There is likely no merging required.
SATYA PAL
2023 年 2 月 22 日
If I am extracting 500 datapoints thens original datas (180 data points) are missing. so I want 320 data within 180 test data
SATYA PAL
2023 年 2 月 22 日
I want to keep 180 exp data points and want to merge 320 interpolated dataset to make it 500.
Star Strider
2023 年 2 月 22 日
Then just concatenate them using the square brackets [] concatenation operator.
Remember to use the semicolon (;) vertical concatenation operator if necessary.
If they are column vectors im a matrix and you want to sort them by one of the columns, use the sortrows function on the matrix. (The order makes no difference to lsqcurvefit, however it would be important if you want to plot them.)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Descriptive Statistics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)