Including look up tables in a formula

1 view (last 30 days)
Hello
I was wondering if it is possible to link to a look up table within a formula. I have a set of constraints for maximum acceleration of a vehicle at a certian speed set in a table for example column 1 is velocity (m/s) and column 2 is maximum acceleration (m/s^2) possible at that speed
Table 1.
V A
10 9.4
20 8.1
30 6.65
40 3.9
I then have an input parameter for the vehicle that gives the actual speed at specific times
Speed{1} = 20
Speed{2} = 20
Speed{3} = 30
Speed{4} = 40
I then would like to use the max acceleration for given speed
so for
i = 1:4
ax(i) = A(i)
Where A(i) should correspond to the appropraite A from Table 1 based on the speed given. Is there a way to write this as a script so Matlab can look up the correct A for corresponding V.
Secondly if so, is it then possible for Speed{n} to be a number not listed (25 fir example) and Matlab to interpolate between the numbers given in Table 1.
Best Wishes
Kieran

Accepted Answer

Adam Danz
Adam Danz on 4 Jun 2019
Edited: Adam Danz on 5 Jun 2019
I propose creating an anonymous function that stores the interpolated table and receives a velocity as input and outputs the associated max acceleration. BTW, if there is a simple function that produces the max acceleration given the velocity, you should use that instead.
% Create the original table
T = table([10 20 30 40]',[9.4,8.1,6.65,3.9]','VariableNames', {'V', 'A'});
% Create an interpolated version
Vinterp = 10 : 0.01 : 40; %Interp'd to 0.01 resolution (which you can change)
Ainterp = interp1(T.V,T.A,Vinterp);
VAmat = [Vinterp',Ainterp']; %easier to work with matrices so no table conversion
% create annonymous function that stores VAmat and does look-up
VAfcn = @(v)VAmat(abs(v-VAmat(:,1))==min(abs(v-VAmat(:,1))),2);
This function works with any precision and merely looks up the nearest value in the velocity column and returns the paired max acceleration. You can pass this anonymous function into other functions and it will retain the interpolated data.
Examples:
>> VAfcn(10.523)
ans =
9.3324
>> VAfcn(35.03)
ans =
5.2668
VAfcn(22.234567)
ans =
7.7767
  14 Comments
Adam Danz
Adam Danz on 1 Jul 2019
Edited: Adam Danz on 1 Jul 2019
It's 7pm where I'm at with ~2 more hours of daylight ;)
Good luck.
Kieran Reeves
Kieran Reeves on 2 Jul 2019
Hi Adam
Works really well. Just though I would let you know.
Thanks once again
Kieran

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!