lscurve fit problem issue in Money Rivlin model
10 次查看(过去 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))

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 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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 (한국어)