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

5 comentarios

Stephen23
Stephen23 el 8 de Mzo. de 2017
How is the "polynomial curve" specified in MATLAB? what kind of variable is it stored in?
Your example A is a vector: in your question you state that you want a matrix. Which do you want?
Luke Crouch
Luke Crouch el 8 de Mzo. de 2017
Thanks for your answer! I added the curve data to my question. The matrix is a 66x151 matrix, if you copy the code to Matlab you should see it.
John D'Errico
John D'Errico el 8 de Mzo. de 2017
This question is totally confusing on many levels, a difficult thing to do in such a short question. So you need to explain clearly what you need and what you have.
You state that you have a polynomial curve. In what form is this curve? We are not shown any polynomial. Only a picture with one curved segment. How is the polynomial stored? What form does it take?
The curve that you show has actually linear segments in it that horizontal or vertical. So the curve that you have drawn is not truly a polynomial, since a polynomial is defined over the entire real line. So is this "curve" really a piecewise thing representing a boundary of some sort?
Next, you talk about things like "to the left of the curve" but since part of the curve appears to be horizontal, what do you really mean?
You talk about needing a 2-d lookup table. But the vector A that you show is a VECTOR. In any terminology that I know of, a 2-d lookup table indicates something that has TWO independent variables, so an approximation to a function of the form z(x,y). Here x and y are independent variables, z a third variable, predicted by TWO independent variables.
You talk about a code example. WHERE?????? All you have shown is a vector that contains a lot of elements that are either 0 or 2. The problem is, in your words: "So the matrix I am trying to create has zeros and ones in it..." So is it 1 or 2? Is it a vector or a matrix? A vector in this context is a long string of numbers, a one dimensional thing. A matrix has TWO dimensions. Which is it? What do you really need?
So, SLOW DOWN. Explain what you need very clearly. Using more words where necessary is not a bad thing. If you have code, then show it. Remember, we know nothing about your problem except what you write and the pictures that you have drawn. Use standard mathematical terms. Be consistent. As otherwise, you sow seeds of confusion when we try to answer your question, when we try to figure out what you really mean.
John D'Errico
John D'Errico el 8 de Mzo. de 2017
Editada: John D'Errico el 8 de Mzo. de 2017
Why would you save it as a text file? Save it as a .mat file, then we can just load it in. I've now done that for you, as a .mat file attached to this comment.
Luke Crouch
Luke Crouch el 8 de Mzo. de 2017
Thanks for taking the time to write this answer John. You are right, my question is very short, I tried to keep it simple. I edited it and I hope you now understand it better.
Regarding your questions: The Matrix is in the attached m-file and a 66x151 matrix and clearly not a vector.

Iniciar sesión para comentar.

 Respuesta aceptada

John D'Errico
John D'Errico el 8 de Mzo. de 2017
Editada: John D'Errico el 8 de Mzo. de 2017

0 votos

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.

3 comentarios

Luke Crouch
Luke Crouch el 8 de Mzo. de 2017
Editada: Luke Crouch el 8 de Mzo. de 2017
Thank you very much, a very well written and informative answer!
I dont need a matrix, I just thought that within a simulink simulation that would be the best solution to this problem.
So I will try to call your curvetest function from the simulink model.
John D'Errico
John D'Errico el 9 de Mzo. de 2017
If it turns out that you do need a matrix, it can be done. It is not even that terribly difficult.
The trick is to use the function I wrote. Then, pass it an entire set of pairs (x,y), as generated by the meshgrid function.
meshgrid will create arrays of all combinations of (x,y) pairs in a lattice. If you pass them into the function I created, it will generate the desired array.
But IF you can simply use the function I gave you, this will be simpler, better, etc. Since I know nothing at all about Simulink, that part is beyond me, except that I'm quite confident that you can do what you want. :)
Luke Crouch
Luke Crouch el 9 de Mzo. de 2017
Its definitely possible and working, so thank you very much! I was thinking that the lookup table might be better in terms of performance of the simulation than calling a matlab function, but Im not sure about that.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 8 de Mzo. de 2017

Comentada:

el 9 de Mzo. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by