matlab, how to plot the "root locus" - matlab

Given a function (call it sys(s)), we can use matlab: rlocus(sys) to plot the root locus of that function.
However,if we are given a function with a parameter (say b), eg sys(s)=(2s+2+b)/s , how can I use matlab to plot the rlocus(sys) as a function of the parameter b?

Let's say b changes between 1 and 100 with intervals of 1.
b = 1:100;
We need to create axes and hold them, so that we can plot root loci on top of each other.
axes();
hold('on');
Now we need to create a transfer function for each b and plot its root locus.
for idx = 1:length(b)
sys = tf([2 2+b(idx)], [1 0]);
rlocus(sys);
end
This is the resulting plot:
I could not find a vectorized solution, so it takes quite a long time. This took 45 seconds on my computer. If you need to calculate many values, you will need a vectorized solution.
To add a legend, you need to create a cell array to store b values.
legendStr = cell(1, length(b));
Then, inside the for loop you need to convert b values to string and store them in legendStr.
legendStr{idx} = num2str(b(idx));
After the for loop add the legend to the plot.
legend(legendStr)

Related

plot a curve for each parameter in a figure by recalling mat file

I am doing something similar to the following example:
t=linspace(0,1);
z=rand(1);
theta=pi*rand(1);
a_range=[1,2,3];
for nplot=1:3
a=a_range(nplot);
x=z*t.^2+a*sin(theta);
end
fname=['xsin.mat'];
save(fname)
the parameter a has three different values. For each value of a, I want to plot the function in one single figure. So at the end I will have three figures. I can do it using subplot but this will generate three plots in one figure which is not what I want.
In a new script, I load the mat file:
load('xsin.mat')
for nplot=1:3
figure(nplot)
plot(t,x)
end
but I get one figure not three as I should have. I mean for a=1, I should plot the curve in figure 1; for a=2, I should plot the curve in figure 2 and so on. How can I do this? Any help is appreciated.
You are overwriting x in every iteration; you could modify your code by saving each x for the respective nplot in a separate column of a matrix X (with minimal changes to your code):
t=linspace(0,1);
z=rand(1);
theta=pi*rand(1);
a_range=[1,2,3];
X = NaN(length(t), length(a_range)); % Create matrix to hold x values
for nplot=1:3
a = a_range(nplot);
x = z * t.^2 + a * sin(theta);
X(:,nplot) = x; % Store x in column of X
end
fname=['xsin.mat'];
save(fname)
Then, to create the figures:
load('xsin.mat')
for nplot=1:3
x = X(:,nplot); % Retrieve x from column of X
figure(nplot)
plot(t,x)
end

storing and accessing with matrices of more than two dimensions

I am using ode45 to solve a system with 4 variables. For each time I execute the code:
[t y] = ode45(#func, tspan, y0);
t will be a one dimensional matrix, while y will be a 2 dimensional matrix, with 4 columns, each of which is the solution for one of the variables in question.
I want to run multiple trials of this, and keep them in a 3D matrix my_y_results and my_t_results. I want to be able to, for example, plot the final value of a certain variable for a particular initial condition, as I change the initial condition. How would I do this?
So, on each iteration of the loop below, I want to place the new values in a new matrix.
for i = 1:1:10
y0 = **some value**
[t_temp, y_temp] = ode45(#func, tspan, y0);
my_t_results = **something**
my_y_results = *something* //your code here
end
Also, how would I access the different values after setting them? For example, to get the last value of the variable y(1) for each of the 10 trials, what code would I use?
Higher dimensions can be accessed similar to the usual row and column dimensions. Let's assume t is Nx1 and y is Nx4, and that we are running M trials (note that each trial will have to have the same number of points, N, in order to store the data in a 3-dimensional array).
Your array my_t_results doesn't have to be 3-dimensional and can simply be NxM, where each column is the time vector for a different trial.
The array my_y_results would be Nx4xM and can be defined in MATLAB with:
my_y_results = zeros(N,4,M);
At the end of each i'th trial you would store the results like this:
my_y_results(:,:,i) = y;
And of course accessing the data is similar:
y_i = my_y_results(:,:,i);

matlab plotting a family of functions

Generate a plot showing the graphs of
y=(2*a+1)*exp(-x)-(a+1)*exp(2*x)
in the range x ∈ <-2, 4> for all integer values of a between -3 and 3
I know how to make typical plot for 2 values and set a range on the axes, but how to draw the graph dependent on the parameter a?
To elaborate on Ben Voigt's comment: A more advanced technique would be to replace the for-loop with a call to bsxfun to generate a matrix of evaluations of M(i,j) = f(x(i),a(j)) and call plot with this matrix. Matlab will then use the columns of the matrix and plot each column with individual colors.
%%// Create a function handle of your function
f = #(x,a) (2*a+1)*exp(-x)-(a+1)*exp(2*x);
%%// Plot the data
x = linspace(-2, 4);
as = -3:3;
plot(x, bsxfun(f,x(:),as));
%%// Add a legend
legendTexts = arrayfun(#(a) sprintf('a == %d', a), as, 'uni', 0);
legend(legendTexts, 'Location', 'best');
You could also create the evaluation matrix using ndgrid, which explicitly returns all combinations of the values of x and as. Here you have to pay closer attention on properly vectorizing the code. (We were lucky that the bsxfun approach worked without having to change the original f.)
f = #(x,a) (2*a+1).*exp(-x)-(a+1).*exp(2*x); %// Note the added dots.
[X,As] = ndgrid(x,as);
plot(x, f(X,As))
However for starters, you should get familiar with loops.
You can do it using a simple for loop as follows. You basically loop through each value of a and plot the corresponding y function.
clear
clc
close all
x = -2:4;
%// Define a
a = -3:3;
%// Counter for legend
p = 1;
LegendText = cell(1,numel(a));
figure;
hold on %// Important to keep all the lines on the same plot.
for k = a
CurrColor = rand(1,3);
y= (2*k+1).*exp(-x)-(k+1).*exp(2.*x);
plot(x,y,'Color',CurrColor);
%// Text for legend
LegendText{p} = sprintf('a equals %d',k);
p = p+1;
end
legend(LegendText,'Location','best')
Which gives something like this:
You can customize the graph as you like. Hope that helps get you started!

Scatter plot grouped by row for a 31 x 3 2d array

I have a 2d array and I want to scatter plot the points grouped by row so that each row has a different symbol. This is my code so far, all the points are the same symbol so I can't tell which points are part of which row.
a = zeros (31,3);
for k = 0:30
y = 5*k
dent = [1 10 10 y]
a(k+1, [1 2 3]) = roots(dent)
end
t = 1:3
gscatter(real(a(:,t)),imag(a(:,t)));
You do not need a loop, you can exploit the gscatter options:
a = zeros (31,3);
for k = 0:30
y = 5*k;
dent = [1 10 10 y];
a(k+1, [1 2 3]) = roots(dent);
end
group = ones(size(a));
group(:,1) = group(:,1).*0;
group(:,3) = group(:,3).*2;
gscatter(real(a(:)),imag(a(:)),group(:),'brg','xo+');
You need an additional vector, group, which contains information on which points in your data-set belong to a specific group. This variable is very versatile, see it's documentation.
In your case, I suggest setting up a matrix that is 0 in the first column, 1 in the second and 2 in the third.
In the gscatter function call, reshape all your matrices into vectors using (:) (because gscatter only works with vectors.
The other two strings passed to gscatter:
'brg'
'xo+'
determine the color and shape of the symbols, respectively. Your plot then looks like this:
EDIT
For those users without access to the gscatter function, this is how it can be done using scatter:
s = 40;
hold on
COLORS='brg';
SYMBOLS='xo+';
for t=1:size(a,2)
scatter(real(a(:,t)),imag(a(:,t)),s,SYMBOLS(t),'MarkerEdgeColor',COLORS(t))
end
hold off
A few things to take note of:
to be used in this way, scatter needs a symbol-size, which was set to s = 40 in this example.
the symbols are stored in a string variable so that the can be called in the loop.
the same is true for the edge colors (face colors could also be specified, check the scatter documentation
when called in a loop, use hold to plot into the same figure (roughly speaking)
This is the output from the standard scatter plot:

4D plot display variables with data cursor Matlab

I am having trouble figuring out how display 4 variables in my plot.
I want to vary the independent variables X,V, to produce the dependent variables Y and Z.
Y is a function of X AND V. And Z is a function of Y AND X.
This may be easier to see the dependencies: X, V, Y(X,V), Z(X,Y(X,V)).
I have used the surf function to plot X,Y,Z, but I also want to know the values of V, which I cannot currently ascertain.
Here is some test data to illustrate:
X = linspace(1,5,5)
V = linspace(1,5,5)
Capture = []
for j = 1:length(V)
Y = X.*V(j)
Capture = [Capture;Y]
end
[X,V] = meshgrid(X,V);
Z = Capture.*X
surf(X,Y,Z)
If I use the data cursor, I can see values of X,Y,Z, but I would also like to know the values of V. I know that the way I have it set up is correct because if I make two plots, say:
surf(X,Y,Z)
surf(X,V,Z)
and then use the data cursor to go on the same point of X and Z for both graphs the values for V and Y are what they should be for that point (X,Z).
Is there anyway to show the values for X,Y,V and Z without having to generate two separate graphs?
Thanks!
Using color as your 4th dimension is a possibility (whether it looks good to you is a matter of taste).
surf(X,Y,Z,V); #% 4th arg (V) is mapped onto the current colormap
You can change the colormap to suit your tastes.
colorbar #% displays a colorbar legend showing the value-color mapping
Edit: The questioner wants to see exactly the data in the not-shown array, rather than just a color. This is a job for custom data cursor function. Below I've implemented this using purely anonymous functions; doing it within a function file would be slightly more straightforward.
#% Step 0: create a function to index into an array...
#% returned by 'get' all in one step
#% The find(ismember... bit is so it returns an empty matrix...
#% if the index is out of bounds (if/else statements don't work...
#% in anonymous functions)
getel = #(x,i) x(find(ismember(1:numel(x),i)));
#% Step 1: create a custom data cursor function that takes...
#% the additional matrix as a parameter
myfunc = #(obj,event_obj,data) {...
['X: ' num2str(getel(get(event_obj,'position'),1))],...
['Y: ' num2str(getel(get(event_obj,'position'),2))],...
['Z: ' num2str(getel(get(event_obj,'position'),3))],...
['V: ' num2str(getel(data,get(event_obj,'dataindex')))] };
#% Step 2: get a handle to the datacursormode object for the figure
dcm_obj = datacursormode(gcf);
#% Step 3: enable the object
set(dcm_obj,'enable','on')
#% Step 4: set the custom function as the updatefcn, and give it the extra...
#% data to be displayed
set(dcm_obj,'UpdateFcn',{myfunc,V})
Now the tooltip should display the extra data. Note that if you change the data in the plot, you'll need to repeat Step 4 to pass the new data into the function.