matlab zplane function: handles of vectors - matlab

I'm interested in understanding the variety of zeroes that a given function produces with the ultimate goal of identifying the what frequencies are passed in high/low pass filters. My idea is that finding the lowest value zero of a filter will identify the passband for a LPF specifically. I'm attempting to use the [hz,hp,ht] = zplane(z,p) function to do so.
The description for that function reads "returns vectors of handles to the zero lines, hz". Could someone help me with what a vector of a handle is and what I do with one to be able to find the various zeros?
For example, a simple 5-point running average filter:
runavh = (1/5) * ones(1,5);
using zplane(runavh) gives an acceptable pole/zero plot, but running the [hz,hp,ht] = zplane(z,p) function results in hz=175.1075. I don't know what this number represents and how to use it.
Many thanks.

Using the get command, you can find out things about the data.
For example, type G=get(hz) to get a list of properties of the zero lines. Then the XData is given by G.XData, i.e. X=G.XData.
Alternatively, you can only pull out the data you want
X=get(hz,'XData')
Hope that helps.

Related

Find minimum and maximum of a two variable function on fixed interval in Matlab, and plotting those points in the same graph with the function

I have this function below and I need to calculate the minimum and maximum of this function U, and also plotting the maximum and minimum value in 3D graph together with the function.
How can I write the code?
[x,y]=meshgrid(0:0.1:pi,0:0.1:2*pi);% x-theta,y-phi
a=90.7;b=36.2;c=12.9;
E=1.44;
U=-E.^2*(a.*sin(x).^2.*cos(y).^2+b.*sin(x).^2.*sin(y).^2+c.*cos(x).^2);
meshc(x,y,U)
xlabel('\theta')
ylabel('\Phi ')
zlabel('U')
I tired this way to find max but I don't know if i did it correct
max(U,[],1) %max row-wise
max(U,[],2) %max column-wise
and for the minimum it didn't work the same idea, also I didn't get the exact value of the maximum
As stated above, to simply find the maximum/minimum of your sampled function, use m = min(U(:)); M = max(U(:)). To be able to plot them, what you are missing are the coordinates that give you those values. For this, you will need the second output of the min/max functions, which gives you the index where the extreme happens.
A possible implementation (possibly not the best one) would be (might not work perfectly, I don't have matlab at hand):
[Ms,I] = max(U,[],1); %row-wise maximum and their indexes
[M,j] = max(Ms); %maximum among all rows and its index
Now i = I(j) is the location of the maximum. You can finally do plot3(x(i,j),y(i,j),U(i,j),'ro') to plot a big red circle in the maximums location, or whatever you like.
Note: I might have it backwards and it might be x(j,i), and so on. Just check. Of course you can do the same thing for min().
EDIT: I just remembered the ind2sub function , which solves all your problems. Following the syntax used above:
[M,ind] = max(U(:));
[i,j] = ind2sub(size(U),ind)
The rest holds the unchanged.
You can simply use something like
max(max(U))
this will find the maximum for your 2D matrix.
For the minimum you just have to replace max with min.

Vector operations on object-array with 'colon' indexing [MATLAB]

In my Matlab code I have an object-array of 'masses' which are an object of a class which describes the mass, speed, accerleration etc.
To speed up the simulation I want to reduce the usage of for-loops with using more vector operations. One of the operations is to get the distance of the current mass to all others.
I would like to solve it like that:
%position is a vector with x and y values e.g. [1 2]
%repeat the current mass as many times as there are other masses to compare with
currentMassPosition = repmat(obj(currentMass).position, length(obj), 2);
distanceCurrentMassToOthersArray = obj(:).position - currentMassPosition;
I cannot use the colon-indexing operation on the object array. Currently i use a for-loop where I iterate through every single object. Do you have any tips to optimise that without using a for loop?
I hope my question was clear enough, otherwise I will optimise it ;).
I used this code to reproduce your problem. For future questions please try to include such examples into your question:
classdef A
properties
position
end
methods
function obj=A()
obj.position=1;
end
end
end
.
%example code to reproduce
x(1)=A
x(2)=A
x(3)=A
%line which causes the problem
x(:).position-3
To understand why this is not working, take a look at the output of x(:).position, just type it into the console. You get multiple ans values, indicating a comma separated list of multiple values. If you use [x(:).position] instead, you get an array of doubles. The correct code is:
[x(:).position]-3

Multiexperiments in iddata function from Matlab's system ID toolbox

I am trying to evaluate with iddata (INFO) in matlab, a number of N_E experiments.
I already computed and have as cell arrays of size 1xN_E the outputs and inputs, y and u respectively. Every entry of the cell arrays y and u is a vector of length N=316 (SISO system). For the sake of correctness, period is also a cell array of size 1xN_E, with the period in every entry.
Using the command:
data = iddata(y,u,period);
doesn't produce the expected averaged data-set. Instead, it is handled as a 361x361MIMO system (!).
I've already tried transposing, without results.
data = iddata(y.',u.',period.');
Does someone know why this happens, and how can I produce the desired multi-experiment data-set?
P.S. the documentation I read is for Matlab R2014b, and I am running R2013b. Does someone know if this was not supported in my edition? Or how can I find out?
Actually, the Matlab documentation provides an answer to my question.
The function iddata is very strict regarding how the dimension of output y, input u and period period are defined.
Defining 1xN_experiments cell arrays for y,u and period (note: same size for all!; also N_experimentsx1 won't be recognized by iddata) and then using iddata:
data = iddata(y,u,period);
gives the desired iddata structure.
Note all vectors within y and u must be of same length(!)

Suppress kinks in a plot matlab

I have a csv file which contains data like below:[1st row is header]
Element,State,Time
Water,Solid,1
Water,Solid,2
Water,Solid,3
Water,Solid,4
Water,Solid,5
Water,Solid,2
Water,Solid,3
Water,Solid,4
Water,Solid,5
Water,Solid,6
Water,Solid,7
Water,Solid,8
Water,Solid,7
Water,Solid,6
Water,Solid,5
Water,Solid,4
Water,Solid,3
The similar pattern is repeated for State: "Solid" replaced with Liquid and Gas.
And moreover the Element "Water" can be replaced by some other element too.
Time as Integer's are in seconds (to simplify) but can be any real number.
Additionally there might by some comment line starting with # in between the file.
Problem Statement: I want to eliminate the first dip in Time values and smooth out using some quadratic or cubic or polynomial interpolation [please notice the first change from 5->2 --->8. I want to replace these numbers to intermediate values giving a gradual/smooth increase from 5--->8].
And I wish this to be done for all the combinations of Elements and States.
Is this possible through some sort of coding in Matlab etc ?
Any Pointers will be helpful !!
Thanks in advance :)
You can use the interp1 function for 1D-interpolation. The syntax is
yi = interp1(x,y,xi,method)
where x are your original coordinates, y are your original values, xi are the coordinates at which you want the values to be interpolated at and yi are the interpolated values. method can be 'spline' (cubic spline interpolation), 'pchip' (piece-wise Hermite), 'cubic' (cubic polynomial) and others (see the documentation for details).
You have alot of options here, it really depends on the nature of your data, but I would start of with a simple moving average (MA) filter (which replaces each data point with the average of the neighboring data points), and see were that takes me. It's easy to implement, and fine-tuning the MA-span a couple of times on some sample data is usually enough.
http://www.mathworks.se/help/curvefit/smoothing-data.html
I would not try to fit a polynomial to the entire data set unless I really needed to compress it, (but to do so you can use the polyfit function).

Gaps In Plot Of Piecewise Function in Matlab

I want to plot a piecewise function, but I don't want any gaps to appear
at the junctures, for example:
t=[1:8784];
b=(26.045792 + 13.075558*sin(0.0008531214*t - 2.7773943)).*((heaviside(t-2184))-(heaviside(t-7440)));
plot(b,'r','LineWidth', 1.5);grid on
there should not be any gaps appearing in
the plot between the three intervals , but they do.
I want the graph to be continueous without gaps.
Any suggestions on how to achieve that.
Thanks in advance.
EDIT
Actually, my aim is to find the carrier function colored by yellow in the figure below. I divide the whole interval into 3 intervals: 1-constant 2-sinusoidal 3- constant, then I want to find the overall function from the these three functions
Of course there are "gaps". The composite function is identically zero for all t<2184, and for all t>7440. The relationships can only be non-zero inside of that interval. And you have not chosen a function that is zero at the endpoints, so how can you expect there not to be "gaps"?
What values does your function take on at the endpoints of the interval?
>> t = [2184 7440];
>> (26.045792 + 13.075558*sin(0.0008531214*t - 2.7773943))
ans =
15.689 20.616
So look at the hat function part of this. I'll be lazy and use ezplot.
>> ezplot(#(t) ((heaviside(t-2184))-(heaviside(t-7440))),[0,8784])
Now, combine this, multiplying it by a trig piece, and of course the result is identically zero outside of that domain.
>> ezplot(#(t) (26.045792 + 13.075558*sin(0.0008531214*t - 2.7773943)).*((heaviside(t-2184))-(heaviside(t-7440))),[0,8784])
But if your goal is some sort of continuous function across the two chosen points in the hat function, you need to chose the trig part such that it is zero at those same two points. Mathematics is not spelled mathemagics. Wishing that you get a continuous function will not make it so.
So is your real question how to chose that internal piece (segment) as one such that the final result is continuous? If so, then we need to know why you have chosen the arbitrary constants in there. Surely these numbers, {26.045792, 13.075558, 0.0008531214, 2.7773943} all must have some significance to you. And if they are important, then how can we possibly make the result a continuous function?
Perhaps, and I'm just guessing here, you want some other result out of this, such that the function is not identically zero outside of those bounds. Perhaps you wish to extrapolate as a constant function outside of those points. But to help you, you must help us.