Exercises
Instructions:
You will need to record the results of your MATLAB session to generate your lab report. Create a directory (folder) on your computer to save your MATLAB work in. Then use the Current Directory ?eld in the desktop toolbar to change the directory to this folder. Now type
diary lab1_yourname.txt
followed by the Enter key. Now each computation you make in MATLAB will be save in your directory in a text ?le named lab1 yourname.txt. When you have finished your MATLAB session you can turn off the recording by typing diary off at the MATLAB prompt. You can then edit this ?le using your favorite text editor (e.g. MS Word).
Lab Write-up: Now that your diary file is open, enter the command format compact (so that when you print out your diary file it will not have unnecessary blank lines), and the comment line
% MAT 275 MATLAB Assignment # 1
Include labels to mark the beginning of your work on each part of each question, so that your edited lab write-up has the format.
Important: An unedited diary file without comments submitted as a lab write-up is not acceptable.
1. All points with coordinates x = r cos (θ) and y = r sin (θ), where r is a constant, lie on a circle with radius r, i.e. satisfy the equation x2 + y2 = r2. Create a row vector for θ with the values 0, π/4 , π/2 , 3π/4, π, and 5π/4.
Take r = 2 and compute the row vectors x and y. Now check that x and y indeed satisfy the equation of a circle, by computing the radius r = √(x2 + y2).
2. Use the linspace command or the colon operator : to create a vector t with 91 elements: 1, 1.1, 1.2, . . . , 10 and define the function y = et/10 sin (t) / (t2 + 1) (make sure you use ";" to suppress the output for both t and y).
(a) Plot the function y in black and include a title with the expression for y.
(b) Make the same plot as in part (a), but rather than displaying the graph as a curve, show the unconnected data points. To display the data points with small circles, use plot (t,y,'o'). Now combine the two plots with the command plot (t,y,'o-') to show the line through the data points as well as the distinct data points.
3. Use the command plot3(x,y,z) to plot the circular helix x(t) = sin t, y(t) = cos t, z(t) = t 0 ≤ t ≤ 20.
NOTE: Use semicolon to suppress the output when you define the vectors t, x, y and z. Make sure you use enough points for your graph so that the resulting curve is nice and smooth.
4. Plot y = cos x in red with a solid line and z = 1 - x2/2 + x4/24 in blue with a dashed line for 0 ≤ x ≤ π on the same plot.
Add a grid to the plot using the command grid on.
NOTE: Use semicolon to suppress the output when you de?ne the vectors x, y and z. Make sure you use enough points for your graph so that the resulting curves are nice and smooth.
5. The general solution to the differential equation dy / dx = x + 2 is
y(x) = (x2/2) + 2x + C with y(0) = C.
The goal of this exercise is to write a function file to plot the solutions to the differential equation in the interval 0 ≤ x ≤ 4, with initial conditions y(0) = -1, 0, 1.
The function file should have the structure function+function (similarly to the M-file myplot1.m
Example 3, page 5). The function that defines y(x) must be included in the same file (note that the function defining y(x) will have two input arguments: x and C).
Your M-file should have the following structure (fill in all the?? with the appropriate commands):
Function ex5
x = ?? ; % define the vector x in the interval [0,4]
y1 = f(??); % compute the solution with C = -1
y2 = f(??); % compute the solution with C = 0
y3 = f(??); % compute the solution with C = 1
plot(??) % plot the three solutions with different line-styles
title(??) % add a title
legend(??) % add a legend
end
function y = f (x, C)
y = ?? % fill-in with the expression for the general solution
end
Plot the graphs in the same window and use different line-styles for each graph. To plot the graphs in the same window you can use the command hold on or use the plot command similarly to Exercise 4.
Add the title 'Solutions to dy/dx = x + 2'.
Add a legend on the top left corner of the plot with the list of C values used for each graph.
(Type help plot for a list of the different line-styles, and help legend for help on how to add a legend.) Include both the M-file and the plot in your report.
NOTE: the only output of the function file should be the graph of the three curves. Make sure you use enough points so that the curves are nice and smooth.
6. (a) Enter the function f (x, y) = x3 + (yex / x+1) as an inline or anonymous function. Evaluate the function at x = 2 and y = -1.
(b) Type clear f to clear the value of the function from part (a). Now write a function M-file for the function f (x, y) = x3 + (yex / x+1). Save the file as f.m (include the M-file in your report). Evaluate the function at x = 2 and y = -1 by entering f(2,-1) in the command window.
Attachment:- MATLAB.pdf