how to find almost equal entries in 2 row vectors?
2 views (last 30 days)
Show older comments
delta =
Columns 1 through 7
-5.0000 -4.5000 -4.0000 -3.5000 -3.0000 -2.5000 -2.0000
Columns 8 through 14
-1.5000 -1.0000 -0.5000 0 0.5000 1.0000 1.5000
Columns 15 through 21
2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000
p42 =
Columns 1 through 7
74.9106 72.4174 69.9589 67.5351 65.1461 62.7918 61.7295
Columns 8 through 14
59.6842 57.6910 55.7487 53.8564 52.0129 50.2170 48.4677
Columns 15 through 21
46.7635 45.1032 43.4854 41.9085 40.3710 38.8713 37.4077
p43 =
Columns 1 through 7
31.9658 33.2883 34.6411 36.0260 37.4448 38.8993 40.3911
Columns 8 through 14
41.9217 43.4927 45.1054 46.7612 48.4613 50.2070 51.9995
Columns 15 through 21
53.8397 55.7288 57.6678 59.6575 60.7376 63.0215 65.3396
p42 and p43 are calculated from delta.. i need to find that value of delta for which p42 and p43 are exactly or almost equal i need a way to find (by interpolating) the almost exact value which these two vectors have in common. i suppose i could use polyfit to get two polynomials that fit these vectors but i dont know how to use polyfit in this case since that needs two data vectors. and even if i could use polyfit i have no idea where to go from there. help
2 Comments
Accepted Answer
dpb
on 19 Oct 2013
Just one line for brevity, same works for entire altho size of outputs grows. Might want to select overlapping subsections via min/max of the two vectors combined to limit the problem space if the vectors are large...
>> p42=[46.7635 45.1032 43.4854 41.9085 40.3710 38.8713 37.4077];
>> p43=[41.9217 43.4927 45.1054 46.7612 48.4613 50.2070 51.9995];
>> [k,d]=dsearchn(p42',p43');
>> [md,ix]=min(d);
>> [p42(k(ix)) p43(ix) md]
ans =
45.1032 45.1054 0.0022
>>
6 Comments
dpb
on 19 Oct 2013
Edited: dpb
on 19 Oct 2013
The general idea is --
Write a function (m-file) that describes the problem--
function F = myfun(x)
F = P42func(x)-P43func(x);
Save this function file as myfun.m somewhere on your MATLAB path.
Next, set up an initial point and options and call fsolve:
x0 = 0; % Make a starting guess at the solution
options = optimset('Display','off'); % Turn off display
[x,Fval,exitflag] = fsolve(@myfun,x0,options);
More details, see
doc fsolve
More Answers (1)
dpb
on 19 Oct 2013
Oh, that's a different problem than I thought you meant--
>> interp1([p42-p43]',delta',0)
ans =
1.0014
>>
Or, use fsolve and the generator to solve directly
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices 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!