Azzera filtri
Azzera filtri

Getting multiple outputs using lqr function

2 visualizzazioni (ultimi 30 giorni)
Avishek  Mondal
Avishek Mondal il 26 Gen 2018
Risposto: Star Strider il 26 Gen 2018
I've got the following matrices, and want to design a LQR controller-
A=[-0.0507 -3.861 0 -32.17; -1.17e-3 -0.5164 1 0;-1.29e-4 1.4168 -0.4932 0;0 0 1 0];
B=[0 -0.0717 -0.1645 0]';
C=[0 0 0 1];
Q=diag([1e-3 1e-2 1 1e-3]);
R=1;
To get the controller parameters-
[K,S,e]=lqr(A,B,Q,R);
I now want to show the effect of using different values of R and plot things, say the value of the gain. I know this can be done using a for loop. However, I understand that Matlab's for loop should probably be avoided as much as possible, and so was wondering, is there a vector method of doing this?
Thanks in advance!

Risposte (1)

Star Strider
Star Strider il 26 Gen 2018
There is no reason to avoid explicit loops. Many MATLAB functions have very efficient implicit loops, so they are preferred over explicit loops.
If you want to avoid an explicit loop, you can use arrayfun. I have not timed the difference between a for loop and arrayfun to see which is faster or more efficient.
Try this:
[Kc,Sc,ec] = arrayfun(@(R) lqr(A,B,Q,R), 1:5, 'Uni',false);
This iterates through ‘R=[1:5]’, and outputs to the cell arrays.
Experiment to get the result you want.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by