You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
lscurve fit problem issue in Money Rivlin model
14 views (last 30 days)
Show older comments
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 Comment
Accepted Answer
Star Strider
on 20 Feb 2023
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 Comments
SATYA PAL
on 20 Feb 2023
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
on 20 Feb 2023
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
on 20 Feb 2023
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
on 20 Feb 2023
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
on 20 Feb 2023
Dear sir
can we solve the problem with '
"operation with incompatible arrays sizes"
Star Strider
on 20 Feb 2023
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
on 21 Feb 2023
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
on 21 Feb 2023
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
on 21 Feb 2023
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
on 21 Feb 2023
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
on 21 Feb 2023
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
on 22 Feb 2023
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
on 22 Feb 2023
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
on 22 Feb 2023
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
on 22 Feb 2023
I want to keep 180 exp data points and want to merge 320 interpolated dataset to make it 500.
Star Strider
on 22 Feb 2023
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.)
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)