In Matlab, create a set of x and y coordinates in which the x coordinates are va
ID: 3540498 • Letter: I
Question
In Matlab, create a set of x and y coordinates in which the x coordinates are values from 1 to 25 in increments of 0.5 and the y coordinates are along the line y = x but
shifted a random amount between -5 and +5. Output these values to a file using a format that will allow it to be read by your Project 4 program. Next, use your Project 4 program to read in the data from this file and produce the LSRL line equation. You will probably need to update your Project 4 program with the filename chosen here. Also, be sure to place the data file appropriately so your
C++ program can open it. Write down the equation for use in the next step. Finally, in another Matlab script, you will need to create a plot that contains both the data points from the file that you generated in the first Matlab script and the LSRL line, which you will generate in this script using the slope and intercept you noted from your Project 4 output. Your plot will be graded on its presentation.At the very least, it should include a title and axes labels, the line should be represented with a line while the data points are not connected, and the two should be different colors.
All adjustments to the plot should be done in the program, not through the Matlab
GUI.
Explanation / Answer
When MATLAB starts (either by typing matlab at the command prompt on a Unix machine, or running the executable on a PC or Mac) the MATLAB prompt
>>
appears. All MATLAB commands are executed from this prompt.
>> 2.3+4.2
ans =
6.5000
By default MATLAB returns numerical expressions as decimals with 5 digits. The format function is used to change the format of the output. Type format rat to have MATLAB return rational expressions.
>> format rat
>> 5.1-3.3
ans =
9/5
To eliminate the extra spacing type format compact.
>> format compact
>> 5*7
ans =
35
The four basic operations of addition, subtraction, multiplication and division are performed using the symbols +,-,* and /, respectively. Exponentiation is performed by means of the symbol ^.
>> 2^7
ans =
128
MATLAB has most standard mathematical functions built-in. The sqrt function computes the square root.
>> format long
>> sqrt(2)
ans =
1.41421356237310
The basic trigonometric functions (cos, sin, tan, sec, csc, cot), their inverses (acos, asin, atan, asec, acsc, acot), the exponential function exp, and the natural logarithm log are also built-in. For instance, ln(4)+cos(p/6) is computed as follows.
>> log(4)+cos(pi/6)
ans =
2.25231976490433
For information about any MATLAB function, type help followed by the name of the function.
>> help abs
ABS Absolute value.
ABS(X) is the absolute value of the elements of X. When
X is complex, ABS(X) is the complex modulus (magnitude) of
the elements of X.
See also SIGN, ANGLE, UNWRAP.
Overloaded methods
help sym/abs.m
To avoid having to retype long expressions use the up arrow key %uFFFD to scroll through lines previously typed. Typing one or more characters and then the up arrow key displays previous lines that begin with those characters. To exit MATLAB type quit.
3 Variables
To assign a value to a variable in MATLAB simply type the name of the variable, followed by the assignment operator, =, followed by the value.
>> x=7
x =
7
Note that variable names in MATLAB are case sensitive, so X and x are not equal. We can perform all of the usual operations with x.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.