For this assignment you are to write a simple Matlab script that will ask the user for a range of X values, evaluate a given equation for each of the values in the range, and then plot the results.
Background and Mathematics
A scientist working in a laboratory has analyzed some data, and determined that ( according to theory ) it should fit the following equation:
sin( ) cos( )
( ) = 4 | | ( 1 )
+2.08∗10 √ln( 1.09 + )
Your task will be to write a Matlab script to evaluate this function, initially for a single value of X and then for a given range of X values, plotting the results in the latter case.
For this assignment you are to write a simple Matlab script that asks the user for a range of X values and then calculates and plots the above function for all of the values in the range.
Your program should first print out your name and ACCC netID ( e.g. jbell ), and explain to the user what the program does.
Your program should then ask the user to input a range of X values, defined by three quantities, given in this order: (1) A minimum value of X, (2) a maximum value of X, and
(3) an incremental value by which X should be changed between successive data points. For example, X might range from -15.0 to 15.0 in steps of 0.1 .
Finally your program should calculate the range of X, calculate f(X) for the range, and plot the results. Your plot should include a title and labelled axes.
It is best not to try to write this whole script in one shot, but rather build it up step by step:
First play with Matlab, and learn to use the math functions that you will need, specifically abs( ), log( ), sqrt( ), sin( ), and cos( ).
Then set X equal to a single value, and try to evaluate the function above for a single value of X. You may want to start out with a simpler version, and then build it up. Note that you will need * to indicate multiplication and probably some extra ( parentheses ) to enforce the order of operations. Check your results using a calculator or other tool.
If you have not already done so, put your equations into a Matlab script. Then modify the script to ask the user for X, instead of using a fixed value.
Next change the script to ask the user for a range of X values, and verify that you can generate the range from the input given. ( This will probably break your equation, but don’t panic. )
Next adjust the equation to work on a vector of X values instead of a simple scalar. Replace
with .* , / with ./ , and ^ with .^ to make the equation operate elementwise on all of the elements in the vector. You should get a vector for F the same dimensions as the original dimensions of the X vector.
Now plot F(X) as a function of X. The easiest way is to start by selecting the two variables in the workspace, right clicking, and then selecting plot from the pull-down menu. You can then copy the plot command into your script and add the title( ), xlabel( ), and ylabel( )
You should work out some sample problems by hand before writing any computer code. You should then verify that your computer program yields the same results.
In addition to the plot, report f(X) for the minimum and maximum X values of the given input range. ( F( max( X ) ), not max( F( X ) ) , and likewise for min( X ). )
Try to format the results as “nicely” as you can. At a minimum there should be enough text to label the results in complete sentences, and units should always be specified. ( I.e. don’t just print numbers or variable names and numbers. )
The basic assignment should not use any loops. You may use loops in your program only if you implement one of the optional enhancements ( see below ), e.g. to allow the user to solve multiple problems without restarting the program. You are also not expected to write separate Matlab functions at this point, though that is allowed.
Since you haven’t learned how to test things yet, you can assume that all user input is good.
What to Hand In:
Your code, including a user documentation file, should be handed in using Blackboard.
All files should be zipped together into a single file, whose name is comprised of your ACCC netID followed by the course number followed by the letters “HW”, followed by the assignment number. ( E.g. jbell109HW3.zip ) The zip file should be handed in via Blackboard. ( Your TA may provide alternate instructions, which override these. )
The intended audience for the documentation file is a general end user, who might want to use this program to perform some work. They do not get to see the inner workings of the code, and have not read the homework assignment. You can assume, however, that they are familiar with the problem domain ( e.g. basic mathematical equations. )
A secondary purpose of the documentation file is to make it as easy as possible for the grader to understand your program. If there is anything special the grader should know about your program, be sure to document it in the documentation file. In particular, if you do any of the optional enhancements, then you need to document what they are and anything special the TA needs to do to run your program and understand the results.
For this assignment, include in your documentation file a sample plot image generated by your program, e.g. for X from -10 to 10 in steps of 0.1
If there are problems that you know your program cannot handle, it is best to document them as well, rather than have the TA wonder what is wrong with your program.
Make sure that your name appears at the beginning of each of your files. Your program should also print this information when it runs.
It is course policy that students may go above and beyond what is called for in the base assignment if they wish. These optional enhancements will not raise any student’s score above 100 for any given assignment, but they may make up for points lost due to other reasons.
Check the data entered to verify that it is valid, e.g. that the maximum is strictly larger than the minimum, and that the increment is positive.
Ask the user if they would like to solve additional problems, and if so, repeat until they indicate they are done.
Other enhancements that you think of – Check with TA for acceptability.
Your plot might look something like the following, over the range from -15 to 15. ( Be aware that your TA will evaluate your program over different range(s), not just the sample range. )