Problem 1: Write Matlab code for plotting as a red line, for 100 evenly spaced points in between and . Choose a plotting function that highlights the functional relationship between and . Label the axes and title the graph.
x = linspace(-10,10,100); y = 1.2*exp(-0.8*x); semilogy(x,y,'r-'); xlabel('x') ylabel('y') title('graph of y = 1.2 exp(-0.8x)'); % one choice of title
Problem 2: Write an equation for as a function of for the following data plot.
We see a straight line on a plot where both axes are logarithmic. That means a power-law relationship, i.e. the form . Increasing by a factor of causes an increase in by a factor of . Therefore . At , , so . Therefore the functional relationship is .
Problem 3: Write Matlab code that produces a contour plot of over the rectangle and , with 30 evenly spaced points in each of and .
x = linspace(-pi,pi,30); y = linspace(-1,1,30); [X,Y] = meshgrid(x,y); Z = (1-Y.^2).*sin(X); contour(x,y,Z); xlabel('x') ylabel('y') title('contours of (1-y^2) sin(x)')
Problem 4: Write Matlab code that produces the solution of the following system of equations
A = [1 -1 0 1; 0 2 3 4; 4 -3 2 1; -1 3 6 0]; b = [2; -5; 3; 11]; x = A\b
Problem 5: Write one line of Matlab code that would verify that the solution computed in the previous problem satisfies the given equations. Hint: you can answer this in as little as five characters, or perhaps even three.
Ax-b
Problem 6: Write a Matlab function that returns a numerical approximation to using the following series, where is an input argument to the function.
function e = eulernumber(N) % return approximation of e from sum_{n=0}^N 1/n! % it's easiest to take care of n=0 term before loop nfactorial = 1; e = 1; for n=1:N nfactorial = n*nfactorial; e = e + 1/nfactorial; end end
Problem 7: The Lorenz system is a ordinary differential equation in three variables defined by
Write Matlab code for an anonymous function that returns the vector given an input vector , using parameter values , , and .
f = @(v) [10*(v(2)-v(1)); v(1)*(28-v(3)) - v(2); v(1)*v(2) - 8*v(3)/3];
Problem 8: Revise your answer to Problem 7 so that the anonymous function is in the right form to be passed into Matlab's {\tt ode45} function, and write Matlab code that would integrate the Lorenz system from to , from the initial condition , , .
f = @(t, v) [10*(v(2)-v(1)); v(1)*(28-v(3)) - v(2); v(1)*v(2) - 8*v(3)/3]; [t,v] = ode45(f, [0 100], [0; 1; 0]);
Problem 9: Suppose you have a biased coin that has a 53.8\% chance of landing ``heads'' on any given toss, and you want to figure out how likely it is that, out of ten coin tosses, you'll get exactly heads, for each value of between 0 and 10. Write a Matlab function that would estimate the probability getting heads in ten tosses.
function p = biasedcoin(n); % estimate prob of getting n heads in 10 tosses of a 53.8\% head biased coin Ntrials = 1e04; count = 0; for n=1:Ntrials flips = rand(10,1); % simulate ten coin flips if sum(flips < 0.538) == n % test if there are n heads in ten flips count = count + 1; end end p = count/Ntrials;
Problem 10: Write Matlab code that would set to a matrix representing the connectivity of the following network of links.
C = zeros(4,4); C(3,1) = 1; C(4,1) = 1; C(1,2) = 1; C(4,2) = 1; C(1,3) = 1; C(4,3) = 1; C(2,4) = 1;