How can i make this signal in MATLAB script to use in lsim command - matlab

i need to use this signal as the u of the lsim function. Since it's a really uneven signal i could only make it with a really messy code using ones and zeros for every interval and i can plot it like that with no problems but when i try to use the same data for t and u of the lsim function i get an error. Is there a different way to make this signal and use it with lsim.
This is the signal i need to use:
Signal
The code i used to plot this signal:
x1=0:.001:1.33;
x2=1.33:.001:2.33;
x3=2.33:.001:2.66;
x4=2.66:.001:3;
x5=3:.001:4.33;
x6=4.33:.001:5.33;
x7=5.33:.001:6;
x8=6:.001:7.33;
x9=7.33:.001:8.33;
x10=8.33:.001:9;
x11=9:.001:10;
y1=ones(size(x1));
y2=zeros(size(x2));
y3=ones(size(x3));
y4=zeros(size(x4));
y5=ones(size(x5));
y6=zeros(size(x6));
y7=ones(size(x7));
y8=zeros(size(x8));
y9=ones(size(x9));
y10=zeros(size(x10));
y11=ones(size(x11));
y=[y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11];
x=[x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11];
plot(x,y)
axis([0 10 -0.3 1.5]);
The code for lsim function:
A=[-7 -14 -8;1 0 0;0 1 0];
B=[6;0;0];
C=[0 0 1];
D=[0];
t=x; //x from the code above
u=y; //y from the code above
x0=[0;0;0];
[y,x]=lsim(A,B,C,D,u,t,x0);
subplot(311);plot(t,x(:,1));
subplot(312);plot(t,x(:,2));
subplot(313);plot(t,x(:,3);``

Related

solve system of linear equations in matlab

I'm new to Matlab. Suppose I want to solve a linear system of 2 equations with 5 variables x1, x2, x3, x4, x5. Can Matlab give me solution for x1 and x2 in terms of the x3, x4, and x5? I also want to assign values to one or more variables, say I want to look at what happens if x3=5 or x3=3 and x5=1. Is there a way to achieve this?
I looked at the help page https://www.mathworks.com/help/symbolic/solve-a-system-of-linear-equations.html#d120e14359, but it does not cover the non-square matrix case
You can use multiple calls of solve to get solutions for x1 and x2. In this problem you can solve the first equation for x1, and then plug that into the second equation to get x2 in terms of x3, x4, and x5. You can then substitute the new value of x2 back into your solution of x1.
The subs function is used to substitute the solved values back into the original equation.
syms x1 x2 x3 x4 x5
eq1 = x1 + 4*x2 - 5*x3 + 2*x4 + x5;
eq2 = 3*x1 + 8*x2 - 3*x3 + x4 - x5;
x1s = solve(eq1, x1); % Solve x1 in term of x2-x5
x2s = solve(subs(eq2, x1, x1s), x2); % Solve x2 in terms of x3-x5
x1s = solve(subs(eq1, x2, x2s), x1); % Resolve x1 in terms of x3-x5
Output:
x1s =
3*x4 - 7*x3 + 3*x5
x2s =
3*x3 - (5*x4)/4 - x5
You can plug in values for x3, x4, and x5 using subs. For example, for x4=3 and x5=4:
subs(x1s, [x4 x5], [3 4])
ans =
21 - 7*x3

quadtree for point data in 2D space in matlab

Let's say: I have the point data in 2D space:
A=[x1 y1 %point 1
x2 y2 %point 2
x3 y3 %point 3
.......
x100 y100] %point 100
Do you have any code to use a quadtree for that data in Matlab? (I tried to search on Google, but all results shown the code for image(picture) only.
Thanks,

TF Plot in MATLAB

I want to divide 5 low frequency of EEG data. Here I am using EMD to obtain that. What I want to ask is how we can create a line which will separate each frequency like this picture below. For example, delta is 0-4 Hz, theta 4-8 Hx, alpha is 8-12 Hz, so on.
This picture is TF Plot obtained from EMD
I am not sure what EMD is, but if that is within Matlab, then you can add the lines to this graph like this:
x1 = 0;
x2 = 0.5;
y1 = 4;
y2 = 8;
y3 = 12;
y4 = 40;
figure; hold on
plot([x1, x2], [y1, y1])
plot([x1, x2], [y2, y2])
plot([x1, x2], [y3, y3])
plot([x1, x2], [y4, y4])
Make sure your graph goes after figure; hold on and it should work.

generateing a 3 dimensional scatter plot in matlab

Consider the following example:
x1 = 13 + 6.*rand(100,1);
x2 = x1.*0.7;
x3 = (x2 + 6).*1.2;
figure(1);
plot(x1);hold on;plot(x2);hold on;plot(x3,'--r');
figure(2);
subplot(311);
scatter(x1,x2);
subplot(312);
scatter(x1,x3);
subplot(313);
scatter(x2,x3);
Here I have 3 vectors that are highly correlated. Is it possible in Matlab to generate one scatter plot that contains all of the information, i.e. a scatter plot that shows the relationship between x1 and x2; x2 and x3; x1 and x3, possibly a 3d scatter plot?
scatter3(x1,x2,x3)
See http://www.mathworks.co.uk/help/matlab/ref/scatter3.html for more details

formatting plotyy with two vectors for each y axis

I have a plotyy figure in MATLAB with 2 vectors in on each y axis.
plotyy(x1,[y1(:),y2(:)], x1,[y3(:),y4(:)])
I need to format each of the lines separately, but can not find the documentation about how to do this. Can someone please me show an example?
Does the following example code help?
%# Generate some data
N = 20;
X = (1:N)';
Y1 = randn(N, 1);
Y2 = randn(N, 1);
Y3 = randn(N, 1) - 50;
Y4 = randn(N, 1) - 50;
%# Perform the plotyy, returning an axes handle, and a handle for both figures
[Axes, fig1, fig2] = plotyy(X, [Y1 Y2], X, [Y3 Y4]);
%# Change the format of Y1 and Y2 (separately)
set(fig1(1), 'LineStyle', ':');
set(fig1(2), 'LineStyle', '--');
%# Change the format of Y3
set(fig2(1), 'LineStyle', '-.');
In the above code, the figure handle fig1 corresponds to the first y-plot, ie Y1 and Y2, and I can access the individual lines by indexing fig1 with 1 and 2.
Similarly, the figure handle fig2 corresponds to the second y-plot, ie Y3 and Y4, and I access Y3 by indexing this handle with 1. I could also access Y4 with fig2(2), if I so desired.