Including look up tables in a formula

1 回表示 (過去 30 日間)
Kieran Reeves
Kieran Reeves 2019 年 6 月 4 日
コメント済み: Kieran Reeves 2019 年 7 月 2 日
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

採用された回答

Adam Danz
Adam Danz 2019 年 6 月 4 日
編集済み: Adam Danz 2019 年 6 月 5 日
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 件のコメント
Adam Danz
Adam Danz 2019 年 7 月 1 日
編集済み: Adam Danz 2019 年 7 月 1 日
It's 7pm where I'm at with ~2 more hours of daylight ;)
Good luck.
Kieran Reeves
Kieran Reeves 2019 年 7 月 2 日
Hi Adam
Works really well. Just though I would let you know.
Thanks once again
Kieran

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by