Random Number Generation - Infection - matlab

InfectionMatrix = zeros(100,100);
InfectionMatrix(50,50) = 1;
for k = 1:1:90
[i,j] = find(InfectionMatrix > 0.7);
%Random Number Generator
a = 0.0270;
b = 0.3250;
RANDOM = (b-a).*rand(8,1) + a;
%%%%%%%%%%%%%%%%%%%%%%%%
i(i == 1) = [];
i(i == length(InfectionMatrix)) = [];
j(j == 1) = [];
j(j == length(InfectionMatrix)) = [];
InfectionMatrix(i-1,j) = InfectionMatrix(i-1,j)+ RANDOM(1);
InfectionMatrix(i-1,j+1) = InfectionMatrix(i-1,j+1)+ RANDOM(2);
InfectionMatrix(i-1,j-1) = InfectionMatrix(i-1,j-1)+ RANDOM(3);
InfectionMatrix(i,j+1) = InfectionMatrix(i,j+1)+ RANDOM(4);
InfectionMatrix(i,j-1) = InfectionMatrix(i,j-1)+ RANDOM(5);
InfectionMatrix(i+1,j+1) = InfectionMatrix(i+1,j+1) + RANDOM(6);
InfectionMatrix(i+1,j-1) = InfectionMatrix(i+1,j-1) + RANDOM(7);
InfectionMatrix(i+1,j) = InfectionMatrix(i+1,j) + RANDOM(8);
rng('shuffle')
TooHigh = find(InfectionMatrix > 100);
for m = 1:1:length(TooHigh)
InfectionMatrix(TooHigh(m)) = 100;
end
end
h = heatmap(InfectionMatrix)
old_warning_state = warning('off', 'MATLAB:structOnObject');
hs = struct(h);
warning(old_warning_state);
hs.XAxis.TickValues = [];
hs.YAxis.TickValues = [];
I want to model kind of a simplistic view of a spreading infection. There are 2 things I'd like to accomplish.
Id like to know how to change the spread of the infection to be more circular, there are clearly issues with the corner nodes progressing in a star shape.
Id also like to know how to change the bottom half of the map to progress at half the rate of the top, i.e The Random number generation RANDOMwould be RANDOM/2
That kind of idea.
I'd appreciate any help to improve my code that is not relevant to the problems I specified also.

I'm not sure what you're aiming for but I think you would want to use a 2d convolution of the sort
Infected = zeros(100);
Infected(50,50) = 1;
InfectionMatrix = zeros(100);
for k = 1:90
RANDOM = (b-a).*rand(3,3) + a;
RANDOM(2,2) = 0;
RANDOM = RANDOM .* fspecial('gaussian',3,1); % sigma=1 3x3 kernel
InfectionMatrix = InfectionMatrix + conv2(Infected,RANDOM,'same');
Infected = InfectionMatrix > 0.7;
end
You can modulate the random number by a gaussian filter, like fspecial(), to ensure that they fall off at the corners.

Related

MATLAB Neural Network - Forward Propagation

I am creating a Forward Propagation In the feedforward step, an input pattern is propagated through the network to obtain an output. I have written this in pseudo code and currently attempting to implement this within MATLAB.
There are two errors I currently receive.
Patterns = x'; Desired = y; NHIDDENS = 1; prnout=Desired;
% Patterns become x so number of inputs becomes size of patterns
[NINPUTS,NPATS] = size(Patterns); [NOUTPUTS,NP] = size(Desired);
%apply the backprop here...
LearnRate = 0.15; Momentum = 0; DerivIncr = 0; deltaW1 = 0; deltaW2 = 0;
% Keeps the tan ordering of the examples of x
Inputs1= [Patterns;ones(1,NPATS)]; %Inputs1 = [ones(1,NPATS); Patterns];
% Weight initialisation
Weights1 = 0.5*(rand(NHIDDENS,1+NINPUTS)-0.5);
Weights2 = 0.5*(rand(1,1+NHIDDENS)-0.5);
TSS_Limit = 0.02;
for epoch = 1:10
% FORWARD LOOP
size(NOUTPUTS)
size(NPATS)
for ii = 0: ii < length(NINPUTS)
NOUTPUTS(ii+1) = NPATS(ii);
% Sets bias to 1
NOUTPUTS(1) = 1;
end
for ii = NHIDDENS: ii < NINPUTS
sum = 0;
for ij = 0: ij < ii
sum = sum + deltaW1(ii,ij) * NOUTPUTS(ij);
NOUTPUTS(ii) = tanh(sum);
end
end
Unable to perform assignment because the
left and right sides have a different
number of elements.
Error in mlpts (line 66)
NOUTPUTS(i+1) = NPATS(i);
i am still new to MATLAB and trying to become use to it.
After iterating through the loop
NOUTPUTS = 0 and the error is displayed. I am confused as I am trying to increment NOUTPUTS with ii by 1 through each loop.
I have been able to create the forward propagation with a loop.
for i =3:NNODES
summ = 0;
for j=1:i-1
summ = summ + weights(i,j) * Node_outputs(j);
end
if i == NNODES
Node_outputs(i) = summ
else
Node_outputs(i) = sigmoid(summ);
end
end
Out = Node_outputs(NNODES);
% BOut = ((Node_outputs(NNODES)) * (1 - Node_outputs));
BOut=zeros(1,6);
DeltaWeight = zeros(6,6);

Numerically solving a pde with changing domain in Matlab

I'm trying to implement a changing domain into my Matlab code which solves a pde numerically. In the given problem, I am modelling the baking of bread. It is a 1D problem where there is a single heat source from above. I am using the heat diffusion equation and for a fixed domain, I have provided my code below which solves it. However, I need to also model the rising of the bread, so over time my domain, 'L' in my code, changes. I am told that I can assume it changes linearly over an hour and that the bread initially fills 80% of the tin. What suggestions do you guys have for how I can account for the rising?
close all
clear all
clc
Tend = 3600; %Time to end after
dt = 1; %Time step size
Nstep = (Tend/dt)+1; %Number of time points
M = 1000; %Number of x points
dx = 1/M; %Size of the x steps
L = 1; %Length of domain
x = [0:dx:L]; %Creating the x points
kdiff = 2.7e-4; %Diffusion constant value
R = kdiff*dt/(dx*dx);
for i = 1:M+1
u(i,1) = 20;
end
for n = 1:Nstep
DIG(1) = 1;
b(1) = 200; %Boundry condition
DIG(M+1) = 1;
LF(1) = 0;
RT(1) = 0;
LF(M+1) = -1; %Insulated boundry
RT(M+1) = 0;
b(M+1) = 0; %Boundry condition
for m = 2:M
DIG(m) = 2+(2/R);
LF(m) = -1;
RT(m) = -1;
b(m) = u(m+1,n) + u(m-1,n) + (-2 + (2/R))*u(m,n);
end
u(:,n+1) = tdma(LF,DIG,RT,b,M+1);
end
plot(x,u(:,1:600:end)

Conditional Loop to Filter Cell Content Values

I am basically having the problem of deleting certain values and keeping others in a cell of cells.
I create a cell called distances and then want to filter out the values based on the two criteria "blockade and blockadeouter". I am unable to do so with the initial cells in the distance cell remaining empty but more importantly the filtering is not working in the sense I see values outside the specified range staying in distance cell.
I want to store the coordinates xinit,yinit,zinit for the distances which satisfy by range property and store these coordinates in the posx,posy,posz.
Not sure of the mistake I am making. All and any help is welcome.
Thanks
kb = 1.38065*10^(-23) ; %Boltzmann Constant
amu = 1.660539040*10^-27; % atomic mass unit
mRb= 85;%mass of Rubidium 85
c = 299792458; % speed of light
e=1.6021892*10^-19;%electron unit charge
epsilon0 = 8.854 * 10^-12;
%=============Minimum Allowable Distance/Blockade Radius=====================
a = 12.8*10^-6;
blockade =14*10^-6;%lower limit for filtering
blockadeouter = blockade + a;%upper limit for filtering
sigmax = 40;
sigmay = 40;
sigmaz = 0;
%==================Number of Scan Steps====================================
npics =50; %number of iterations
%=============Number of initial particles in the excitation volume in the MOT Stage===================
numberofparticles =100;
%=============Creating a cell system for importing GPT Data into===========
distances = cell(npics,1);%Collecting all distances between particles
posx = cell(npics,1);
posy = cell(npics,1);
posz = cell(npics,1);
for n = 1 : 1 : npics
fprintf('%d ',n);
%Declaration of orgin for simulation
mux = 0; muy = 0; muz =0;
%Creating a x,y,z coordinate system for the ion
xinit = normrnd(mux,sigmax*10^-6,[1 numberofparticles]);
yinit = normrnd(muy,sigmay*10^-6,[1 numberofparticles]);
zinit = normrnd(muz,sigmaz*10^-6,[1 numberofparticles]);
for ii = 1 : 1 : numberofparticles
for jj = ii+1 : 1 : numberofparticles
distances{ii}{jj} = sqrt((xinit(ii) - xinit(jj)).^2 + (yinit(ii) - yinit(jj)).^2 + (zinit(ii) - zinit(jj)).^2);
if (distances{ii}{jj} >= blockade) && (distances{ii}{jj} <= blockadeouter)
posx{n}{jj} = xinit(jj);
posy{n}{jj} = yinit(jj);
posz{n}{jj} = 0;
end
end
end
end % end of npics loop
figure
for ii = 1:1:length(distances)
trial{ii} = cell2mat(distances{ii,1}(1,:));
end
trial = cell2mat(trial);
grid on;
title('Filtered Distances')
hist(trial)
I am not sure what was the problem with the conditional statement but i was able to solve the problem by doing the following:
for ii = 1 : 1 : numberofparticles
for jj = ii+1 : 1 : numberofparticles
distances{ii}{jj} = sqrt((xinit(ii) - xinit(jj)).^2 + (yinit(ii) - yinit(jj)).^2 + (zinit(ii) - zinit(jj)).^2);
if distances{ii}{jj} > blockadeouter
distances{ii}{jj} = [];
end
if distances{ii}{jj} < blockade
distances{ii}{jj} = [];
else
posx{n}{jj} = xinit(jj);
posy{n}{jj} = yinit(jj);
posz{n}{jj} = zinit(jj);
end
end
end

Local Interest Point Detection using Difference of Gaussian in Matlab

I'm writing the code in Matlab to find interest point using DoG in the image.
Here is the main.m:
imTest1 = rgb2gray(imread('1.jpg'));
imTest1 = double(imTest1);
sigma = 0.6;
k = 5;
thresh = 3;
[x1,y1,r1] = DoG(k,sigma,thresh,imTest1);
%get the interest points and show it on the image with its scale
figure(1);
imshow(imTest1,[]), hold on, scatter(y1,x1,r1,'r');
And the function DoG is:
function [x,y,r] = DoG(k,sigma,thresh,imTest)
x = []; y = []; r = [];
%suppose 5 levels of gaussian blur
for i = 1:k
g{i} = fspecial('gaussian',size(imTest),i*sigma);
end
%so 4 levels of DoG
for i = 1:k-1
d{i} = imfilter(imTest,g{i+1}-g{i});
end
%compare the current pixel in the image to the surrounding pixels (26 points),if it is the maxima/minima, this pixel will be a interest point
for i = 2:k-2
for m = 2:size(imTest,1)-1
for n = 2:size(imTest,2)-1
id = 1;
compare = zeros(1,27);
for ii = i-1:i+1
for mm = m-1:m+1
for nn = n-1:n+1
compare(id) = d{ii}(mm,nn);
id = id+1;
end
end
end
compare_max = max(compare);
compare_min = min(compare);
if (compare_max == d{i}(m,n) || compare_min == d{i}(m,n))
if (compare_min < -thresh || compare_max > thresh)
x = [x;m];
y = [y;n];
r = [r;abs(d{i}(m,n))];
end
end
end
end
end
end
So there's a gaussian function and the sigma i set is 0.6. After running the code, I find the position is not correct and the scales looks almost the same for all interest points. I think my code should work but actually the result is not. Anybody know what's the problem?

Using Heat Equation to blur images using Matlab

I am trying to use the PDE heat equation and apply it to images using Matlab. The problem i am having is that the image isn't blurring , it is just going white. Also, I am getting different results from the rest of the class who is using Maple.
Here is the code:
% George Lees Jr.
% Heat equation
clear,clc;
dx = 1;
dy = 1;
dt = .025;
%dt/(dx*dx)
t = 0;
time = 3;
T_old = imread('tulipgray.jpg');
T_temp=T_old;
[m,n,k] = size(T_temp);
%colormap gray;
%imagesc(T_temp);
%imshow(T_old);
T_new = T_temp;
T_new=ind2gray(T_new,colormap);
%T_new(:,50)=0;
%T_old(1,70)
%imagesc(T_new);
%diff_x = dt/(dx*dx)
%diff_y = dt/ (dy*dy)
%time = 0;
while t < time
for i = 2:1:m-1
for j = 2:1:n-1
T_new(i,j) = T_temp(i,j) + dt*(T_temp(i+1,j) -2*T_temp(i,j) + T_temp(i-1,j)) + dt*(T_temp(i,j+1)-2*T_temp(i,j) + T_temp(i,j-1));
t = t+dt;
T_temp(i,j) = T_new(i,j);
end
end
end
figure
imshow(T_new)
Yeah the image just gets whiter
There's 2 issues with your code:
1) you're incrementing the time counter after each individual pixel instead of after doing the whole image
2) you need to do the calculations on floating points values, not integers. dt is small, so the values from the RHS of the equation are <1
Fixed code should look something like this
clear,clc;
dt = 0.025;
time = 3;
T_old = imread('rice.png');
T_temp=double(T_old);
[m,n,k] = size(T_temp);
T_new = double(T_temp);
T_new=ind2gray(T_new,colormap);
while t < time
for i = 2:1:m-1
for j = 2:1:n-1
T_new(i,j) = T_temp(i,j) + dt*(T_temp(i+1,j) -2*T_temp(i,j) + T_temp(i-1,j)) + dt*(T_temp(i,j+1)-2*T_temp(i,j) + T_temp(i,j-1));
end
end
T_temp = T_new;
t = t+dt;
imshow(uint8(T_new))
getframe;
end