MATLAB: Create a matrix from a curve

simulink lookup table

I am trying to create a 2D Lookup Table from a curve.
I have this curve that is the basis for a transmission control unit of an electric vehicle. The inputs are torque and rotational speed.
When the current input is left or under the curve, the output shall be zero, otherwise two. I created a matrix manually, that I am using in a lookup table, which you can find attached. This works fine but to create similiar matrices I would like an easier and better way.
Any help is greatly appreciated!
Luke

Best Answer

Ok. So you have a CURVE, not a polynomial. A polynomial is something with a very specific definition in mathematics. So when you use that word, it implies something that you do not appear to have. In turn, that confuses people trying to answer your question.
plot(Curve_x,Curve_y)
Next, it appears that you want to create a matrix that will act as a 2-d lookup table. So, for ANY (x,y) pair you wish to use a simple index into the table to indicate is a point lies above(right) or below(left) that curve.
The fact is, this is something simply and trivially done using nothing more than a call to interp1. You never even need to create that lookup table.
For example, consider the (x,y) pair (x0,y0) = (2000,57).
x0 = 2000;
interp1([Curve_x,4095],[Curve_y,-1],x0)
ans =
44.4959999999999
So, if y0 is greater than 44.4959999999999 then the (x0,y0) pair is to the right/above the curve.
We can now write the entire operation as simply as this:
curvetest = @(x0,y0,Curve_x,Curve_y) interp1([Curve_x,4095],[Curve_y,-1],x0) < y0;
curvetest(2000,57,Curve_x,Curve_y)
ans =
logical
1
curvetest(2000,23,Curve_x,Curve_y)
ans =
logical
0
Yes, I know this returns 0 or 1. You SAID 1. Not 2, even though that matrix contains 0 or 2. Trivially fixed of course. Just multiply the result by 2, if you really wanted 2 instead of 1.
In fact, curvetest is actually nicely vectorized.
curvetest(2000,30:50,Curve_x,Curve_y)
ans =
1×21 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
curvetest(2000:100:3000,30,Curve_x,Curve_y)
ans =
1×11 logical array
0 0 0 0 1 1 1 1 1 1 1
You can test multiple points at once, as above, or like this:
curvetest([1000, 2000, 3500],[15 25 35],Curve_x,Curve_y)
ans =
1×3 logical array
0 0 1
So, do you really need any more than that? As you can see, I made it a function of the vectors Curve_x and Curve_y. So, if they change, then the function will see that change. Also, I made it go out to x=4095, which seems logical, although you can set that value to anything you want.
Do you REALLY need a matrix result? Yes, it is easy to do. But that also requires you to scale your inputs, and then to use interpolation on the matrix, so a call to interp2 anyway. While all of that can be made to be fast enough, the call to interp1 will still probably be as fast, unless you code it all very carefully.
So, IF you truly need a matrix result, and the function I showed you how to write is not sufficient, then I'll show you how to do it. But I'll observe that often someone thinks they know exactly what they want, when something simpler is sufficient, and in fact, better.