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
Mathieu NOE 2021년 10월 14일
hello Marina
it would be easier f you could share the data
tx
Lotmeri
Lotmeri 2021년 10월 14일
Of course.
y=[92.41, 91.69, 90.93, 90.48, 89.48, 88.33, 88.76, 88.18, 86.56, 85.54, 84.35, 82.73, 84.35, 81.08, 76.25;
97.18, 96.01, 96.08, 93.94 , 94.23, 93.94, 93.05, 92.81, 91.41, 90.77, 89.61, 88.18, 86.17, 84.31,81.00;
97.80, 96.97, 96.51, 95.85, 95.32, 94.72, 94.19, 93.63, 92.59, 91.45, 90.29, 88.53, 86.80, 84.69, 81.33];
x=[8.20, 7.92, 7.63, 7.32, 6.99, 6.63, 6.23, 5.80, 5.31, 4.77, 4.15, 3.42, 2.55, 1.46, -7.71e-15];
f=10*log10(linspace(270,300,3001));
I think that for adjust the dimensions I should use a "repmat"...

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt J
Matt J 2021년 10월 14일
편집: Matt J 2021년 10월 14일

1 개 추천

Assuming f is 3x1,
x=rand(1,15); %Example data
f=rand(3,1);
y=3*x+2+7*f;
[F,X]=ndgrid(f,x);
p=num2cell( [X(:), X(:).^0, F(:)]\y(:) );
[a,b,c]=p{:}
a = 3.0000
b = 2.0000
c = 7

추가 답변 (1개)

Alan Weiss
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
Lotmeri 2021년 10월 14일
Can you please provide me an example? I never used it...
I have a set of points that I have to fit according the equation ax+b+cf, where x and f are noted (but different lengths) and a,b,c not. Thus I suppose that the minimization is y-ax-b-cf=0.
Alan Weiss
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
Lotmeri
Lotmeri 2021년 10월 14일
편집: Lotmeri 2021년 10월 15일
Ok, thank you for your answer. This is exactly my problem: the dimensions. I'll try to explain better myself.
First of all, I need to find a,b,c as scalars.
y is a matrix containg 3 sets of 15 elements, thus I put them together --> 3x15 .
x is a vector of the distance, thus is a vector of 15 distances corresponding to 15 positions. --> 1x15
f is a vector of the frequencies. The 3 sets in y are corresponding to 3 different frequencies (along the 15 positions). The frequencies are not constants, even if in y are taken the 3 sets only for 3 frequencies. So, for now I can either assume that I'll have a f vector of 3 elements, corresponding to the 3 sets, or a matrix of 3 vectors corresponding to the real frequencies.
y [3x15]= a*x[1x15] + b+ c*f [1x3] or [3x3001]
Hope it will help to understand better.
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
Lotmeri 2021년 10월 14일
Thank you so much, you understood the problem. Thus I assume that this objective is the one to use in the Toolbox that you cited above, right?
Alan Weiss
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
Lotmeri 2021년 10월 14일
I tryed with the Optimization Workflow but when I define the Objective of the problem it seems that it accepts only "Objective must be a scalar OptimizationExpression or a struct containing a scalar OptimizationExpression."
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
Lotmeri 2021년 10월 14일
Ok thank you so much. It works!

댓글을 달려면 로그인하십시오.

제품

질문:

2021년 10월 14일

편집:

2021년 10월 15일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by