Starting value of x so it knows when to start looking

• 20
• 88% (8) 7 out of 8 people found this document helpful

This preview shows page 7 - 12 out of 20 pages.

%starting value of x so it knows when to %start looking for intersections k = 2; %starting index for the values of the %intersections %Loops for calulating intersections and deleting repeats and zeros while j<=10 %while the x values are less than 10 zeros(k) = fzero(F3,j); %find zero with guess value of j if abs(zeros(k)-zeros(k-1))>0.0005 %eliminates same answer build up xvalues(k) = zeros(k); %changes name of zeros to xvalues end j = j+0.2; %take an x value step of 0.2 k = k+1; %goes to the next index in array end m=1; %starting value for the index of m for n = 1:length(xvalues)-1 %n is equal to the length of xvalues array if xvalues(m+1) < xvalues(m) %if the next value is bigger than the %previous value... xvalues(m+1) = []; %deletes the time that xvalues are 0 m = m-1; %goes back to the same index it was before %the loop end m = m+1; %goes to the next index of xvalues end
Week 5 Homework ENGR 112 Fzero while xvalues(1) = []; %eliminates first value of x from xvalues %array %Setting up intersections plots y1 = F1(xvalues); %makes a new function for the intersections %using the F1 function y2 = F2(xvalues); %makes a new function for the intersections %using the F2 function %Printing, Plotting, Titles, and Labels plot(xvalues, y1, "b*" ); %plots intersections values title( "Intersections of two functions" ); %titles function for what is in quotes xlabel( "x" ); %labels x axis ylabel( "F(x)" ); %labels y axis fprintf( "The x values for each intersection found are:\n" ) fprintf( "%f\n" ,xvalues) Function Files % Copy and paste your functions here ( if any were used ). Must be size 10, same as MATLAB font and color. Command Window Output The x values for each intersection found are: 4.178573 5.134904 5.759869 7.387417 7.601074 9.460246 9.538232 Answers to question(s) asked in the homework (if any)
Week 5 Homework ENGR 112 Fzero while Extra credit responses: a) Matlab does not like ==0 because it can use decimal places up to many many places. If it were to run with ==0 it would take much much longer for Matlab to finish the command, or it would run error because no value would equal 0 b) If there is only 1 zero, no matter what you guess, it will always return that one value because that is the only root. It doesn’t return what is close to your guess, rather what is CLOSEST to your guess. Image Output
Week 5 Homework ENGR 112 Fzero while Problem 3 Epidemic Part 5 Comments for grader/additional information (if any) I messed up in problem 4 so I had to go about it without loops in order to get the right answer. Script File
Week 5 Homework ENGR 112 Fzero while %Will Frisch, 10/30/19 clear;clc;close; %Initial Values a = 10; %average number of people a person comes in contact with b = 1.25; %amount of time that a person is infectious (days) h = 0.05; %time steps (seconds) nsteps = 140; %number of time steps time = zeros(1,141); %empty matrix for time %Empty Matrix for S and starting values of S (# of people) SStart = zeros(5,141); %Makes an empty matrix for S values to fill SStart(2) = 500; %Sets starting value of S SStart(3) = 1000; %Sets starting value of S SStart(4) = 1500; %Sets starting value of S SStart(5) = 2000; %Sets starting value of S %Starting values of I & R (# of people) IStart = 100*ones(5,141); %Sets starting value of I RStart = zeros(5,141); %Sets starting value of R %Empty Matrix for N and starting values of N (# of people) N = 100*ones(5,141); %Makes an empty matrix for N values to fill N(2,:) = 600; %Sets starting value of N
• • • 