Question
matlab I do not know how to change my code This is my code: clc clear %the known value a=10; b=1.25; h=0.05; R=0; I=100; S=
matlab
I do not know how to change my code
This is my code:
clc clear %the known value a=10; b=1.25; h=0.05; R=0; I=100; S= 0:500:2000; %range N=S+I+R; %Total for i=1:140; %loop 140 time I1= I+(h*(((a.*S.*I)./N)-(I/b))); %equation S1= S+(h*((-a.*S.*I)./N)); R1=R+(h*(I/b)); I=I1; S=S1; R=R1; i=i+1; time=i*h; % time % make the plot for S I R Total subplot(2,2,1) hold on xlabel('Time') ylabel('Number of the people') title('infected versus time') plot(time,I,'x') %make the point subplot(2,2,2) hold on xlabel('Time') ylabel('Number of the people') title('Recovered vesus time') plot(time,R,'x') %make the point subplot(2,2,3) hold on xlabel('Time') ylabel('Number of the people') title('Susceptibles vesus time') plot(time,S,'x') %make the point subplot(2,2,4) hold on xlabel('Time') ylabel('Number of the people') title('Total vesus time') plot(time,N,'x') %make the point end %When S= 0 500 1000 1500 2000 start=0:500:2000; for(i2=1:1:5) fprintf('Start S= %0.f, Ending After %0.f Days: S= %0.f , I= %0.f , R= %0.f , N=%0.f ',start(i2),time,S(i2),I(i2),R(i2),N(i2)); i2=i2+1; end
Function Descriptions . The first function, DiseaseStep, runs the calculations for one time step. . The second function, DiseaseSimulate, calls DiseaseStep nSteps times to simulate the epidemic o DiseseSimulate should call DiseaseStep. Note that you can calculate the starting N value Splitting up code this way lets you use DiseaseStep elsewhere if need be. Having DiseaseSimulate over time. It also creates the time array (see implementation note in previous week). +' using SStart and Start(BStart is zero). * return arrays is handy for plotting.+ Your script should call DiseaseSimulate once with each starting value for S (use a for loop). To-do:* Replace one step of the calculation with DiseaseStep. Make sure it works. The input parameters (Sin, lIn, Rln, N, a, b, h) should be single values, not arrays. Same with the outputs.+ 1. a. First create a function file (new->function file). Copy the line above into the function file; you can change variables names if you want, but b. Go to your script and copy JUST the code that calculates the next step i. Change the variable names - change the input variable names to matclh you need the seven input and 3 output variables given here. into your function file your copied code AND change the output variable names to match your copied code 1. Note: If you used array indices (eg s (k+1)s(k)) when doing DiseaseStep you'l1 need to make these be variables, eg sQut and sIn ii. Put a break point in the first line of your DiseaseStep code c. Take a moment to put comments into your function file > d. Now copy the entire first line of your function file (the bit shown above) e. Paste it into your original script right before the bit of code you copied OUT of the script - i. Comment out the code that you had copied OUT of your script : f. Delete the function keyword - i. If you were doing array indices you will need to put them back in, g. Put a break point in your script right at the DiseaseStep function > h. Run your script. Before stepping into the Diseasesten function make sure that all your INPUT variables are defined i. Step into your DiseaseStep function and make sure there's no errors/missing variables, ets j. When you step out of DiseaseStep your SQut ets should be the new values k. Make sure your code still works exactly the same as it did before EORE doing DiseaseSimulate 2. Replace the simulation for loop with DiseaseSimulate. Make sure it works. Again, the input parameters (SStart, IStart, a, b, h, nSteps) should be single values, not arrays. However, the output should all be arrays of the same size (e.g., SvsT is Susceptible versus time for the nSteps)- function SvsT, Tvst, Rvst, time] - TStart, a, b, h, oSteps,) a. Follow the same steps as you did for DiseaseStep. Create the function, copy the code in from the original script, fix the variable names, then copy the function definition back into your original script (don't forget to delete the function keyword). b. The input variables SStart, TStart should be single values, NOT arrays c. The output variables are arrays d. There should be no plot commands within Diseasesten or 3. 4. Make sure your modified script works with the DiseaseSimulate function Now make the final edit to your original script to use the saved SvsT etc values to do the plots Implementation note: +- You're essentially moving the calculation part of your for loop from part 3 into DiseaseSimulate and splitting out the actual update step into DiseaseStepl. J Your for loop for this week should loop over the different starting values, getting the arrays from DiseaseSimulate and then plotting them. ^ Use a second for loop to add the title, axes, labels, and legend to the graph. To make this work, you'll need to make an array of strings (you can just copy this bit of code):+ . . titleName = { ' susceptibles' , for k-1:4 ' Infected ' , ' Recovered' , ' Total ' ); % (labels, axes, legend for the subplot) , title(strcat (titleNamek}, versus time'; end Self-Check: Start S=0, Ending after 7.00 Days : S - 0, 1-0, R = 100, total 100 Start S-500, Ending after 7.00 Days : S 0, 1-2, R = 5xx, total 600 Start S-1000, Ending after 7.00 Days: S- 0, I - 5, R- XXX5, total 1100 Start S-1500, Ending after 7.00 Days:S-0, I-7, R -XXX3, total 1600 Start S-2000, Ending after 7.00 Days: S = 0, 1 = 10, R = XXX0, total 2100, ENGR112 Full-term assignmente Start S Start S StartS =0 2000 - Start S 0 2000Start S 2000 Start S = 500 Start S-1000 Start S 500 Start S 1000 Start S = 1500 Start S = 2000 Start S 500 Start s-1000 5 -Start S = 1000 -Start S 1 500 -Start S-1500 -Start S = 1500 - tatS20001800 Start S-2000 1800 1800 -Start S = 2000 1600 1600 1600 1600 1400 400 1400 400 1200 200 1200 1200 1000 1000 1000 1000 800 800 600 600 600 400 400 400 200 200Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started