Linear fitting with 2 variables
이전 댓글 표시
Hi everybody,
I'm trying to solve a linear fitting using Matlab. In particular my function is: y=a*x+ b+ c*f, where I have y that it is a matrix 3x15, x is a vector 1x15 and f is a vector 1x3001.
Is there any particular function that is able to fit the coefficients a,b,c directly? Also taking into account that the vectors are not all of the same lenght.
Thank you so much.
PS. I do not have the GADS_Toolbox (for the function createOptimProblem).
댓글 수: 2
Mathieu NOE
2021년 10월 14일
hello Marina
it would be easier f you could share the data
tx
Lotmeri
2021년 10월 14일
채택된 답변
추가 답변 (1개)
Alan Weiss
2021년 10월 14일
1 개 추천
I think that the Problem-Based Optimization Workflow would help. Write your optimization variables a, b, and c as you have done, then set the objective as the minimization of the sum of squares of differences between your data and your result using the optimization variables (I cannot be more specific here because I do not understand what you are etrying to minimize).
Alan Weiss
MATLAB mathematical toolbox documentation
댓글 수: 9
Lotmeri
2021년 10월 14일
Alan Weiss
2021년 10월 14일
I do not understand your equation. You say y is 3x15, and f is 1x3001, so what dimension is c and how is it multiplied by f to get something the dimension of y so that you can add or compare them? Once we have that nailed down we can discuss what your objective function might be, to minimize the sum of squared deviations, but at the moment I don't understand what is deviating.
Alan Weiss
MATLAB mathematical toolbox documentation
Alan Weiss
2021년 10월 14일
I'm sorry, but I do not understand your problem. Your equation is not an equation in any sense that I recognize. I know that you want a least-squares solution to something, but I do not understand to what.
Let's try one more thing. Is it possible that f could be 3x1? And that the first row of y (consisting of 15 elements) corresponds to the first element of f and to x? Similarly, the second row of y corresponds to the second element of f? In that case, I believe that the solution is in hand.

So in the problem-based approach, after you define your variables a, b, and c, and your data y = 3x15, x = 1x15, and f = 3x1, you can define your objective function as
ex1 = a*[x;x;x];
ex2 = b*ones(3,15);
ex3 = c*repmat(f,1,15);
objective = sum((y - ex1 - ex2 - ex3).^2,'all');
Alan Weiss
MATLAB mathematical toolbox documentation
Lotmeri
2021년 10월 14일
Alan Weiss
2021년 10월 14일
Yes, create an optimization problem and optimization variables, then include the objective in the problem. The steps are all laid out in Problem-Based Optimization Workflow.
Alan Weiss
MATLAB mathematical toolbox documentation
Lotmeri
2021년 10월 14일
Alan Weiss
2021년 10월 14일
The objective I wrote is a scalar optimization expression. What did you write?
You could also try this expression:
objective = sum(sum((y - ex1 - ex2 - ex3).^2));
Alan Weiss
MATLAB mathematical toolbox documentation
Lotmeri
2021년 10월 14일
카테고리
도움말 센터 및 File Exchange에서 Choose a Solver에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!