# interpolation for 2 numbers

2 views (last 30 days)
sri satya ravi on 7 Jan 2017
Answered: Star Strider on 7 Jan 2017 If i have an image something like this I want to know the 0.98th value of max power. That occurs on either side of the curve. How can I get the corresponding X-axis for 0.98*max_power.
max_power = 523.6947
##### 2 CommentsShowHide 1 older comment
sri satya ravi on 7 Jan 2017
Please look at the data...... I also attached a figure explaining what i wanted..I want to find the interpolated x values at the condition (0.98*max) indicated by 2 red marks in the figure.
Thanks

Star Strider on 7 Jan 2017
Here you go:
The Code
D = load('sri satya ravi matlab.mat');
RPM = D.RPM;
Power = D.Power;
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
Pwr98Idx = zci(Power - 0.98*max(Power));
for k1 = 1:numel(Pwr98Idx)
RPMPwr98(k1) = interp1(Power(Pwr98Idx(k1)+(-1:1)), RPM(Pwr98Idx(k1)+(-1:1)), 0.98*max(Power), 'linear');
end
figure(1)
plot(RPM, Power)
hold on
plot(RPMPwr98, [1 1]*0.98*max(Power), 'rp', 'MarkerFaceColor','g')
hold off
grid
celtxt = regexp(sprintf('RPM = %.1f\n', RPMPwr98), '\n', 'split');
text(RPMPwr98, [1 1]*0.98*max(Power), celtxt(1:end-1), 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
The Plot The code is simple and straightforward, so I did not comment-document it. See the documentation on the individual functions for the details on how to use them.
The only special function is my ‘zci’ anonymous function. It detects zero crossings here by subtracting 98% of the maximum power from the power vector to create the zero-crossing indices, that are negative values in a vector (created by multiplying the vector by a 1-position shifted version of itself) that is otherwise positive. It then uses those indices to determine where to centre the interpolation, does the interpolation, and stores the results in the ‘RPMPwr98’ vector.
Experiment with the code and the function to see how it works.