Plot discrete points and some circles that enclose them in matlab - matlab

I'm trying to plot some eigenvalues along with their Gershgorin circles in matlab, but don't seem to be able to find the syntax to get the discrete points (the eigenvalues) to show up. This is what I've tried:
clear all ;
m = [ 1 -1 0 0 ;
-1 2 -1 0 ;
0 -1 2 1 ;
0 0 -1 1 ]
e = eig( m ) ;
n = 30 ;
z1 = zeros( n + 1, 1 ) ;
z2 = zeros( n + 1, 1 ) ;
for i = [ 1 : n + 1 ]
z1(i) = 2 + 2 * exp(j * 2 * pi * (i - 1)/ 30) ;
z2(i) = 1 + exp(j * 2 * pi * (i - 1)/ 30) ;
end
h = plot( real(e(1)), imag(e(1)), real(e(2)), imag(e(2)), real(e(3)), imag(e(3)), real(e(4)), imag(e(4)), real(z1), imag(z1), real(z2), imag(z2) )
set(h(1),'LineWidth',2) ;
set(h(2),'LineWidth',2) ;
set(h(3),'LineWidth',2) ;
set(h(4),'LineWidth',2) ;
Which produces a plot in which I can see the circles, but not the points:
If I use the same set command on h(5) or h(6) it does make the circle plots show up thicker as I would have expected.

Well it does not show up because of the call to plot to plot points (horrible sentence sorry!). It's fine if you use scatter.
I modified a bit your code, that's why this is not a comment haha.
1) I vectorized your for-loop, which is quite faster on my computer. BTW, using i as an index is risky, especially when dealing with complex numbers. The safe way to go is either use something else, or
2) use 1j or 1i to represent the imaginary unit. That's also faster.
Anyhow here is the code with the points bigger:
clear
clc
close all
m = [ 1 -1 0 0 ;
-1 2 -1 0 ;
0 -1 2 1 ;
0 0 -1 1 ]
e = eig( m ) ;
n = 30 ;
%// see below for vectorized version
% for k = [ 1 : n + 1 ]
% z1(k) = 2 + 2 * exp(1j * 2 * pi * (k - 1)/ 30) ;
% z2(k) = 1 + exp(1j * 2 * pi * (k - 1)/ 30) ;
% end
%// vectorized loop with 1j as imaginary unit.
z1 = 2 + 2 * exp(1j * 2 * pi * ((1:n+1) - 1)/ 30) ;
z2 = 1 + exp(1j * 2 * pi * ((1:n+1) - 1)/ 30) ;
%// plot the circles and then use scatter for the points.
plot(real(z1), imag(z1), real(z2), imag(z2));
hold on
scatter(real(e),imag(e))
hold off
which gives the following:
You can of course customize the scatter plot as you wish. Hope that helps!

Try this:
h = plot( real(e), imag(e), 'x', real(z1), imag(z1), real(z2), imag(z2) )
More info in the plot documentation.

Related

how do i create a binary [0,1]?

i want to declare that x will be variable for binary 0,1
i and k represent facilities in flow matrix, j and q represent location in distance matrix ..
x(i,j) mean that x will be equal to 1 if i (facility) is assign in j (location) ..
x(i, j) = 1 if facility i is assigned to location j and if otherwise, xij = 0,
so otherwise mean that if x(k,q) =1 , x(i,J) will be 0...
example of the manual calculation
Min =(f i1,k1 * d j1,q1 * x i1,j1 * x k1,q1) + (f i1,k1 * d j1,q2 * x i1,j1 * x k1,q2) + (f i1,k1 * d j1,q3 * x i1,j1 * x k1,q3)....
( 0*0* 1*1 ) + ( 0* 6* 1*0 ) + ( 0*8 * 1 *0 ).....
I want to *xi1,j1 * xk1,q1 to be 0 or 1.. if i choose i1, j1=1 the other will be 0.. for example i2,j1 will be equal to 0
below is the coding
clc;
clear;
%sum sum sum sum(fik*djq*xij*xkq)
%i,k= facilities
%j,q= location
%f(i,k)= flow between facilities i and k
%d(j,q)= distance between locations j and q
%xij = 1 if facility i is assigned to location j and if otherwise, xij = 0
% Flow matrix: flow assigning facility i (column) to facility k (row)
f = [0 5 7 9;
5 0 4 6;
7 4 0 3;
9 6 3 0];
%Distance matrix: distance assigning location j (column) to location q (row)
d = [0 6 8 9;
6 0 5 1;
8 5 0 2;
9 1 2 0];
z= 0;
nf= 4;
nd= 4;
for i=1:nf
for j=1:nf
for k=1:nd
for q=1:nd
z = min('z','f(i,k)*d(j,q)*x(i,j)*x(k,q)');
end
end
end
end
%Constraints
%The first set of constraints requires that each facility gets exactly one
%location, that is for each facility, the sum of the location values
%corresponding to that facility is exactly one
Constraints.constr1 = sum(x,2) == 1;
%The second set of constraints are inequalities. These constraints specify
%that each office has no more than one facility in it.
Constraints.constr2 = sum(x,1) == 1;
disp (z);
by using recperm
this recperm is used to fine permuation 0,1 in the coding

Dividing two polynomials in MATLAB [duplicate]

This question already has answers here:
Divide two polynomials using MATLAB
(2 answers)
Closed 6 years ago.
I'm currently working on a Cyclic Code program for a class in MATLAB and I'm trying to figure out the best way to divide two polynomials, the generator P(X) and the user input, shifted by 3 (x^3 * D(X)) in order to get the Quotient Q(X) and Remainder C(X), which would allow me to get the transmitted data T(X) = X^3*D(X) + C(X)
The code I have for my program so far takes in the users 4-bit input in binary, i.e.
Insert 4-bit input: 1001
Then it checks it to make sure its valid, and shifts it giving:
0 0 0 1 0 0 1
which stands for the polynomial
X^3 + X^6
I then need to divide that by the generator polynomial
P(X) = 1 + X + X^3
Working it out on paper,
x^6 + X^3
___________
x^3 + x + 1
Gives: Q(X) = X^3 + X
R(X) = X^2 + X
So, T(X) = X^6 + X^3 + X^2 + X, which is 0111001 for the Codeword
What would be the best way to do this?
I have tried the following:
% Prompt for User input
b4_in = input('Insert 4-bit input: ' ,'s'); %Input 1001
%% CHECK FOR VAILD INPUT %%
dec_in = bin2dec(b4_in)
bin_in = fliplr(de2bi(dec_in)) %User input in Binary
d = [0000000]; %Calculating X^3 * D(X)
d = bin_in;
d(7)=0;
d = fliplr(d); %Gives 0 0 0 1 0 0 1
d
gen_pol = [1 1 0 1] %P(X) = 1 + X + X^3
[q, c] = deconv(bin_in, gen_pol)
When I go this, I get:
q =
1
c =
0 -1 0 0
What do I need to do differently to get the following?
q = 0 1 0 1
c = 0 1 1
Thank you!
In MATLAB, polynomials read in a binary vector from left to right. For example, x^3+x is [1 0 1 0], x^2+x is [1 1 0]. The Quotient Q(X) should be x^3-x instead of x^3+x. Make sure your inputs are in the right format, and you should get the following result as expected,
q =
1 0 -1 0
c =
0 0 0 0 1 1 0

Solving a linear system of equation with two variables in MATLAB

It might seem a simple question. I need it, though. Let's assume we have two equations:
2 * y + x + 1 = 0
and
y - 2 * x = 0
I would like to find their bisection which can be calculated from this equation:
|x + 2 * y + 1| |-2 *x + y |
------------------- = -----------------
(sqrt(2^2 + 1^2)) (sqrt(1^2 + 2^2))
To make the long story short, we only need to solve this below system of equation:
2 * y + x + 1 = -2 *x + y
and
2 * y + x + 1 = 2 *x - y
However, using solve function of MATLAB:
syms x y
eqn1 = 2 * y + x + 1 == -2 *x + y ;
eqn2 = 2 * y + x + 1 == 2 *x - y ;
[x, y] = solve (eqn1 , eqn2, x, y) ;
Will give me:
x = -1/5 and y = -2/5
But, I am looking for the result equations, which is:
y = -3 * x - 1 and 3 * y = 2 * x - 1
So, does anyone know how I can get the above line equation instead of the result point? Thanks,
The following should solve both equations with y on the left-hand-side:
y1 = solve(eqn1,y)
y2 = solve(eqn2,y)
Result:
y1 =
- 3*x - 1
y2 =
x/3 - 1/3
As an aside, it would be much faster to solve this system by thinking of it it as a matrix inversion problem Ax=b rather than using MATLAB's symbolic tools:
A = [1 2; -2 1];
b = [-1; 0];
x = A\b
Result:
x =
-0.2000
-0.4000

How to formulate this expression

I am new to MATLAB and I want to formulate the following lease square expression in Matlab. I have some codes that I am typing here. But the optimization problem solution seems not to be correct. Does anyone has an idea why?
First, I want to solve the heat equation
$$T_t(x,t) = - L_x . T(x,t) + F(x,t)$$
where L_x is Laplacian matrix of the graph.
then find y from the following least square.
$$ \min_y \sum_{j} \sum_{i} (\hat{T}_j(t_i) - T_j(t_i, y))^2$$
Thanks in advance!!
Here is my code:
%++++++++++++++++ main ++++++++++++++++++++
% incidence matrix for original graph
C_hat = [ 1 -1 0 0 0 0;...
0 1 -1 0 0 -1;...
0 0 0 0 -1 1;...
0 0 0 1 1 0;...
-1 0 1 -1 0 0];
% initial temperature for each vertex in original graph
T_hat_0 = [0 7 1 9 4];
[M_bar,n,m_bar,T_hat_heat,T_hat_temp] = simulate_temp(T_hat_0,C_hat);
C = [ 1 1 -1 -1 0 0 0 0 0 0;...
0 -1 0 0 1 -1 1 0 0 0;...
0 0 1 0 0 1 0 -1 -1 0;...
0 0 0 1 0 0 -1 0 1 -1;...
-1 0 0 0 -1 0 0 1 0 1];
%
% initial temperature for each vertex in original graph
T_0 = [0 7 1 9 4];
%
% initial temperature simulation
[l,n,m,T_heat,T_temp] = simulate_temp(T_0,C);
%
% bounds for variables
lb = zeros(m,1);
ub = ones(m,1);
%
% initial edge weights
w0 = ones(m,1);
% optimization problem
% w = fmincon(#fun, w0, [], [], [], [], lb, ub);
%++++++++++++++++++++ function++++++++++++++++++++++++++++
function [i,n,m,T_heat,T_temp] = simulate_temp(T,C)
%
% initial conditions
delta_t = 0.1;
M = 20; %% number of time steps
t = 1;
[n,m] = size(C);
I = eye(n);
L_w = C * C';
T_ini = T';
Temp = zeros(n,1);
% Computing Temperature
%
for i=1:M
K = 2*I + L_w * delta_t;
H = 2*I - L_w * delta_t;
%
if i == 1
T_heat = (K \ H) * T_ini;
%
t = t + delta_t;
else
T_heat = (K \ H) * Temp;
%
t = t + delta_t;
end
% replacing column of T_final with each node temperature in each
% iteration. It adds one column to the matrix in each step
T_temp(:,i) = T_heat;
%
Temp = T_heat;
end
end
%++++++++++++++++++ function+++++++++++++++++++++++++++++++++++++++++
function w_i = fun(w);
%
for r=1:n
for s=1:M_bar
w_i = (T_hat_temp(r,s) - T_temp(r,s)).^2;
end
end
To give a more clear answer, I need more information about what form you have the functions F_j and E_j in.
I've assumed that you feed each F_j a value, x_i, and get back a number. I've also assumed that you feed E_j a value x_i, and another value (or vector) y, and get back a value.
I've also assumed that by 'i' and 'j' you mean the indices of the columns and rows respectively, and that they're finite.
All I can suggest without knowing more info is to do this:
Pre-calculate the values of the functions F_j for each x_i, to give a matrix F - where element F(i,j) gives you the value F_j(x_i).
Do the same thing for E_j, giving a matrix E - where E(i,j) corresponds to E_j(x_i,y).
Perform (F-E).^2 to subtract each element of F and E, then square them element-wise.
Take sum( (F-E).^2**, 2)**. sum(M,2) will sum across index i of matrix M, returning a column vector.
Finally, take sum( sum( (F-E).^2, 2), 1) to sum across index j, the columns, this will finally give you a scalar.

matlab: praticle state simulation

Lets say I want to simulate a particle state, which can be normal (0) or excited (1) in given frame. The particle is in excited state f % of time. If the particle is in excited state, it lasts for ~L frames (with poisson distribution). I want to simulate that state for N time points. So the input is for example:
N = 1000;
f = 0.3;
L = 5;
and the result will be something like
state(1:N) = [0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 ... and so on]
with sum(state)/N close to 0.3
How to do that?
Thanks!
%% parameters
f = 0.3; % probability of state 1
L1 = 5; % average time in state 1
N = 1e4;
s0 = 1; % init. state
%% run simulation
L0 = L1 * (1 / f - 1); % average time state 0 lasts
p01 = 1 / L0; % probability to switch from 0 to 1
p10 = 1 / L1; % probability to switch from 1 to 0
p00 = 1 - p01;
p11 = 1 - p10;
sm = [p00, p01; p10, p11]; % build stochastic matrix (state machine)
bins = [0, 1]; % possible states
states = zeros(N, 1);
assert(all(sum(sm, 2) == 1), 'not a stochastic matrix');
smc = cumsum(sm, 2); % cummulative matrix
xi = find(bins == s0);
for k = 1 : N
yi = find(smc(xi, :) > rand, 1, 'first');
states(k) = bins(yi);
xi = yi;
end
%% check result
ds = [states(1); diff(states)];
idx_begin = find(ds == 1 & states == 1);
idx_end = find(ds == -1 & states == 0);
if idx_end(end) < idx_begin(end)
idx_end = [idx_end; N + 1];
end
df = idx_end - idx_begin;
fprintf('prob(state = 1) = %g; avg. time(state = 1) = %g\n', sum(states) / N, mean(df));
The average length of the excited state is 5. The average length of the normal state, should thus be around 12 to obtain.
The strategy can be something like this.
Start in state 0
Draw a random number a from a Poisson distribution with mean L*(1-f)/f
Fill the state array with a zeroes
Draw a random number b from a Poission distribution with mean L
Fill the state array witb b ones.
Repeat
Another option would be to think in terms of switching probabilities, where the 0->1 and 1->0 probabilities are unequal.