How to find a variable in a function that mostly match a set of data points
2 views (last 30 days)
Show older comments
I have a one-variable function and a set of data points. The variable of the function is x.
If I mannuly change the variable from 1.0 to 3.0, the function's result will be closer to the data points I have as shown in the pictures below.


I was wondering what kind of MATLAB function I can use to automatically find the best value of x to match the function to the data points the most?
1 Comment
Thanks for asking. If possible. I remain at your disposal for any questions.
Gracias por preguntar. Si es posible. Quedo a su disposición para cualquier consulta.
Accepted Answer
Star Strider
on 30 Jul 2020
First, I would experiment with fminsearch, since it uses a derivative-free approach. With one parameter, this should not be difficult.
For more difficult problems, I tend to favor the ga function. I have no idea how difficult your problem is.
With either of those, the function you would use would be something like this:
fcn = @(x) norm(ydata(:) - yourFunction(x,xdata(:));
This would be easier to respond to with the function and the data.
.
13 Comments
Star Strider
on 3 Aug 2020
Edited: Star Strider
on 3 Aug 2020
I did something like this:
function b = Rp2_fcn(Rp2)
wo = 2*pi*13.56*10^6;
Rsource = 50; Rload = 50;
Rp1 = 0.2; Rp4 = 0.2
L1 = 106e-9; L4 = 106e-9;
C1 = 1/(wo^2*L1); C4 = 1/(wo^2*L4);
% Rp2 = 1.6; % The only variable is Rp2
Rp3 = Rp2;
L2 = 1.84e-6; L3 = 1.84e-6;
C2 = 1/(wo^2*L2); C3 = 1/(wo^2*L3);
d23_array = []; S21_array = []; i = 0;
S21_real = [0.028 0.066 0.126 0.237 0.339 0.468 0.589 0.631 0.596 0.519 0.447 ...
0.367 0.305 0.249 0.209]; % The data points
for d23=10e-3:10e-3:150e-3
i = i+1;
Z11 = Rp1+Rsource+1i*wo*L1-1i/(wo*C1);
Z22 = Rp2+1i*wo*L2-1i/(wo*C2);
Z33 = Rp3+1i*wo*L3-1i/(wo*C3);
Z44 = Rp4+Rload+1i*wo*L4-1i/(wo*C4);
M12 = MI_loop_spiral_50mm_Ben();
M34 = MI_loop_spiral_50mm_Ben();
M23 = MI_Tx_Rx_Ben(d23);
Z12 = 1i*wo*M12;
Z34 = 1i*wo*M34;
Z23 = 1i*wo*M23;
% VL/VS
Numerator = Z12*Z23*Z34*Rload;
Denominator = -Z11*Z22*Z33*Z44-Z12^2*Z34^2+Z11*Z22*Z34^2+Z11*Z44*Z23^2+Z33*Z44*Z12^2;
VL_over_Vs = Numerator./Denominator;
S21 = 2*VL_over_Vs*(Rsource/Rload)^0.5; % The function
d23_array(i) = d23;
S21_array(i) = abs(S21);
end
b = norm(S21_array - S21_real);
end
I remember making other changes in the code to make it more efficient, however I do not remember what they were.
the fminsearch call was essentially:
Rp2 = fminsearch(Rp2_fcn, rand);
EDIT — (3 Aug 2020 at 19:08)
Corrected typographical errors.
.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!