Question

Unable to correct errors from my MatLab Script and would like to see a script to...

Unable to correct errors from my MatLab Script and would like to see a script to compare mine to.

Write a MatLab script that simulates a virus spread.

This problem needs the following parts, some of which are nested loops, ie Part 3, 4 and 5 are nested in Part 2’s loop (for or while):

To build the program first to only deal with infection transmission within the town’s neighbors

  1. An array which for every person in the town which saves the value that corresponds to infection. -1 if recovered, 0 if never sick, 1-14 if infected, when the value =15 it should change to -1 recovered. The array has as many elements (at least, see 2 below) as townspeople.

  1. A loop which checks every day, all townspeople, and for any infected person and increments their infection value in the array from Part 1, from 1 to 2, 2 to 3, etc until they reach day 15, at which the value is set to -1 (recovered). I checked first if they were already infected, and incremented the infection value, then I ran the inner loop from “Part 3” looking for new infections, then at the very end of this Part 2 loop, I counted the number of infected townspeople, and saved the number of infected in an array “NumInfected(d)”, where d is the day.

  1. Within the “Day” loop above, we need a loop that compares each townsperson to their neighbors, i=1:1000, neighbors to the left and then to the right, and infects them if the random number generator requires it.

The person E(i-1), lives to the left, and E(i+1) lives to the right. Each day, for all 1000 townspeople, check first to see if the ith person E(i) is never infected (value E(i)=0), then check their neighbors. If E(i-1) is infected, compute the probability of infection and if it is > 1 then the ith person becomes infected E(i)=1. Check If E(i) =0, and now check whether E(i+1) is infected, compute the probability again, and if the probability is>1 then again E(i)=1.

When i=1 only check the E(i+1) neighbor, and i=1000 only check the E(i-1) neighbor, as they only have 1 neighbor. See “Part” 4 for how to check the probability of infection.

  1. To check the probability of infection of ith person given an infected adjacent neighbor, use floor(P*rand). Set P=1.05, for this, “rand” produces 0 to 1. The value (P*rand) will be >1 if the rand function returns anything greater than 0.95, which will happen 1/20 times. The function floor( ) finds the lowest integer of (P*rand), so floor(P*rand)=0 if rand returns <0.95. If floor(P*rand)>0 the ith person will be infected by the infected neighbor. Every time you call rand, it will return another random number, 0 to 1.

  1. Now plot NumInfected(d) versus d the day.

If this works, you should get some neighbor to neighbor transmission which dies off relatively quickly. If you change P from 1.05 (1/20 odds of transmission per day during infection period) to P = 1.2, you should get more sustained infection numbers.

Now, you can add the classes, there are several ways to do this.

  1. In my program, I added a column to E, so E has a column whose E(ith,1) value corresponds to the infection value, -1,0,1-14 and a column whose E(ith,2) value corresponds to the classroom the person is in, 0,1, 2 (if using the revised specification) or 0,1,2,3,4 (with the original specification). Example for townsperson number 20, E(20,1)=14 on the 14th day of infection, E(20,2)=1 if the person is in class 1, but E(20,2)=2 if the person is in class 2.
  2. You can distribute the students across the first 1-25 or 26-50, etc townspeople, or across the town’s first 500, second 500, it is your choice. You can take two classes and distribute them across the whole town, so that every 40th townsperson is a student, write a for loop from x:1:25 and E(x*40,2)=1 for class 1, E((x*40-20),2) = 2 for class 2. It is up to you, how to distribute the students, but you must distribute them.
  3. This could be called Part 2.5. Before checking for neighborhood transmission, count the number of students in Class 1, 2 who are infected. NumInfClass(1), NumInfClass(2), etc This will require either a loop for each class, which checks all students in the class, or it can be a loop through the town, that adds all of the infected who are also in Class 1, Class 2, etc.
  4. This can be added to Part 3, or added before or after Part 3. For all never infected students in a class, the probability of infection is now (for class 1), floor(NumInfClass(1)*P*rand), if this is >1, the student is infected, so the student can be infected by neighbors, and by other students in their class, and their odds of being infected increase with each infected classmate.

Now plot NumInfected(d) versus d the day.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

townspeople = randi([0 14],1,1000); %assigning random infection value to 100 people.

pr=0;
pl=0;
hold on;
for d=1:25
numberinfected=0;
for i=1:1000
if townspeople(i)>0
townspeople(i)=townspeople(i)+1;%changing infection sttus
numberinfected=numberinfected+1;%changing number of infected status
end
if townspeople(i)==15
townspeople(i)= -1;
numberinfected=numberinfected-1;
end
if townspeople(i)==0 && i~= 1 && i~=1000
if townspeople(i-1) >0
pl=floor(1.05*rand);
end
if townspeople(i+1) >0
pr=floor(1.05*rand);
end

%pr is probability of infection due to right neighbour.

%pl is probability of infection due to left neighbour.

if pr >= 1 || pl >= 1
townspeople(i)=1;
numberinfected=numberinfected+1;
pr=0;
pl=0;
end
elseif townspeople(i)==0 && i== 1
if townspeople(i+1) >0
pr=floor(1.05*rand);
end
if pr >= 1
townspeople(i)=1;
numberinfected=numberinfected+1;
pr=0;
end
elseif townspeople(i)==0 && i== 1000
if townspeople(i-1) >0
pl=floor(1.05*rand);
end
if pl >= 1
townspeople(i)=1;
numberinfected=numberinfected+1;
pl=0;
end
  
end
end
plot(d,numberinfected,'*');

%% Plot the number of infected people for every day(***hold must be on***)
end

Output:

Second Part

townspeople = randi([0 14],1,1000);%assigning random infection value to 100 people.

%assigning class status

for k=1:1000
class(k)=0;
end

for k=20:20:1000
class(k)=2;
end

for k=40:40:1000
class(k)=1;
end

infclass1=0;
infclass2=0;
pr=0;
pl=0;
hold on
for d=1:25
numberinfected=0;
for i=1:1000
if townspeople(i)>0
if class(i)==1
infclass1=infclass1+1;
elseif class(i)==2
infclass2=infclass2+1;
end
  
townspeople(i)=townspeople(i)+1;%changing infection sttus
numberinfected=numberinfected+1;%changing number of infected status
end
if townspeople(i)==15
townspeople(i)= -1;
numberinfected=numberinfected-1;
end
if townspeople(i)==0 && i~= 1 && i~=1000
if townspeople(i-1) >0
pl=floor(1.05*rand);
end
if townspeople(i+1) >0
pr=floor(1.05*rand);
end

%determing class status for infected townpersons(***different varible named class is used***)

%pr is probability of infection due to right neighbour.

%pl is probability of infection due to left neighbour.

%pclass is probability of infection due to Classmates.
if class(i)==1
pclass=floor(1.05*infclass1*rand);
elseif class(i)==2
pclass=floor(1.05*infclass2*rand);
else pclass=0;
end
if pr >= 1 || pl >= 1 || pclass >=1 %checking whether the probability is more than 1 or not
townspeople(i)=1;
numberinfected=numberinfected+1;
pr=0;
pl=0;
pclass=0;
end
elseif townspeople(i)==0 && i== 1
if townspeople(i+1) >0
pr=floor(1.05*rand);
end

if class(i)==1
pclass=floor(1.05*infclass1*rand);
elseif class(i)==2
pclass=floor(1.05*infclass2*rand);
else pclass=0;
end
if pr >= 1 || pclass >=1  %checking whether the probability is more than 1 or not
townspeople(i)=1;
numberinfected=numberinfected+1;
pr=0;
end
elseif townspeople(i)==0 && i== 1000
if townspeople(i-1) >0
pl=floor(1.05*rand);
end

if class(i)==1
pclass=floor(1.05*infclass1*rand);
elseif class(i)==2
pclass=floor(1.05*infclass2*rand);
else pclass=0;
end


if pl >= 1 || pclass>1
townspeople(i)=1;
numberinfected=numberinfected+1;
pl=0;
end
  
end

end
plot(d,numberinfected,'*');
xlabel('Day');
ylabel('Number of Infected persons');
end

Output:

Add a comment
Know the answer?
Add Answer to:
Unable to correct errors from my MatLab Script and would like to see a script to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I NEED THE CODE FOR PART 2-MATH MODELING CLASS USING MATH LAB in thrve posed elevator...

    I NEED THE CODE FOR PART 2-MATH MODELING CLASS USING MATH LAB in thrve posed elevator problem, your assistant notified you that there are 60 workers operating on each of 5 floors atop the ground floor in your office building - 300 in total. The elevator occupancy is 10 people, and there are 3 elevators in total. In the worst-case scenario in terms of total transition time, on each elevator ride at least one person is getting off at each...

  • MATLAB help! I have some MATLAB Assignment to do, the proffesor requires all the small parts...

    MATLAB help! I have some MATLAB Assignment to do, the proffesor requires all the small parts in order to get full credit. Help me out, thank you f LAB SECTION NAME HW6P3 (30 points) following are infinite series representations of the function, un pra i a script file HW6P3 that determines the value of the function from the sum of the series for a given value of x. The program (1 pt) must prompt the user to enter a value...

  • Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void...

    Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void main(String[] args) { int currentVertex, userChoice; Scanner input = new Scanner(System.in); // create graph using your WeightedGraph based on author's Graph WeightedGraph myGraph = new WeightedGraph(4); // add labels myGraph.setLabel(0,"Spot zero"); myGraph.setLabel(1,"Spot one"); myGraph.setLabel(2,"Spot two"); myGraph.setLabel(3,"Spot three"); // Add each edge (this directed Graph has 5 edges, // so we add 5 edges) myGraph.addEdge(0,2,9); myGraph.addEdge(1,0,7); myGraph.addEdge(2,3,12); myGraph.addEdge(3,0,15); myGraph.addEdge(3,1,6); // let's pretend we are on...

  • I REALLY need numbers 2 and 3 and 5 by like tomorrow morning. I have no...

    I REALLY need numbers 2 and 3 and 5 by like tomorrow morning. I have no clue how to do these. I know the image quality is iffy but please help as best you can Homework1 STA4322 Homework 1, Spring 2019 Please turn in your own work, though you may discuss the problems with classmates, the TA, the Professor, the internet, etc. The most important thing is that you understand the problems and how they are solved as they will...

  • Exercise 2. Use R script please Advanced Math use R studio I would like to see...

    Exercise 2. Use R script please Advanced Math use R studio I would like to see a very well documented pdf file with your comments on the steps, and the complete information on your generated sample. Thank you! NOTE for R users: Please export your generated sample as a .csv file (or Rdata file if you prefer). You can find some useful info here. Exercise 2 (20pt). Generate a random sample of size 25 from a normal distribution with mean...

  • Problem 3 (13 pts): (modified from Attaway Problem 3.39) a) (5 pts) Using the Matlab rand () func...

    Problem 3 (13 pts): (modified from Attaway Problem 3.39) a) (5 pts) Using the Matlab rand () function, create a script that generates a row vector (1 row X 20 columns) of random numbers, each with magnitude between 0 and 5 b) (3 pts) Use the round() function to turn all of the values into whole numbers by rounding down (truncate the decimal part on your vector) c) (5 pts) Add code to your script to generate a new vector...

  • Programming in java In this part of this Lab exercise, you will need to create a...

    Programming in java In this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple manipulation of an array. Then you should add a main method to this class to check that your code does what it is supposed to do. 1. Create a BlueJ project named LAB06 as follows: a. Open the P drive and create a folder named Blue), if it does not exist already. Under this folder,...

  • I would just like to check my work on this. Using bioinformatics. 1. a) Find Accession...

    I would just like to check my work on this. Using bioinformatics. 1. a) Find Accession NG_016141.1. what is the name of this sequence and on what chromosome is it located? b) The e-value represents the probability that the sequence you are testing does NOT match the sequence in the database. Let’s say you have a sequence from humans that did not have a known function. It matches to the GFP sequence from jellyfish with an e-value of 1e-2 and...

  • During lab 4, we have seen numerical implementation of Fourier Series for periodic signals. As first part of this assignment, you need to write a Matlab function that would take an array representing...

    During lab 4, we have seen numerical implementation of Fourier Series for periodic signals. As first part of this assignment, you need to write a Matlab function that would take an array representing a single period of a signal (x), corresponding time array (t), and return the Fourier Series coefficients (Ck) in exponential form. The function should also be able to take two (2) optional input arguments: number of Fourier coefficients (Nk) and plot option (p). Use the template ‘fourier_series_exp.m’...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT