代写 html math matlab scala graph software FreeMat/Octave/MatLab and Vectors 1 *Slightly revised version of “Matlab and Vectors” by David Arnold, College of the Redwoods, August 16, 1998.

FreeMat/Octave/MatLab and Vectors 1 *Slightly revised version of “Matlab and Vectors” by David Arnold, College of the Redwoods, August 16, 1998.
In this exercise you will learn how to enter and edit vectors in FreeMat (http://freemat.sourceforge.net/) or Octave (https://www.gnu.org/software/octave/) — these are free, cross-platform, Matlab (https://www.mathworks.com/products/matlab.html) – like program for doing mathematics. Operations involving vectors and scalars will be discussed.
Entering Row Vectors
Entering row vectors is easy in FreeMat. Enter the following command at the FreeMat prompt “>>”.
>> v=[1,2,3]
There are a number of FreeMat constructs introduced in this command. To enter a vector, first enter a left bracket, the elements of the vector separated by commas or spaces, and a closing right bracket.
>> v=[4 5 6]
The = sign is FreeMat assignment operator. You use this operator to assign FreeMat items to variables. To see that the row vector [1 2 3] has been assigned to the variable v, enter the following command at the FreeMat prompt.
>> v
Ranges
Sometime you will need to enter a vector of equally spaced entries. This is easily accomplished in FreeMat with the construct start:increment:finish. If you don’t supply an increment, FreeMat assumes that the increment is 1.
>> x1=0:10
You can select your own increment.
>> x2=0:2:10
You can even go backwards.
>> x3=10:-2:0
And you can get very fancy indeed.
>> x4=0:pi/2:2*pi
There will be times, particularly when plotting functions, that you will need a large number of entries in a vector.
>> x=0:0.1:10
Suppressing Output
You can suppress the output of a FreeMat command by appending a semicolon.
>> x=0:0.1:10;
This is particularly useful when the output is large and you have no desire to see it.
Sizes of Vectors
You can find the size of the vector v by entering
>> size(v)
The information supplied by the result of this last command indicates that the vector v has 1 row and 3 columns. Although you can think of the vector v as a matrix with 1 row and 3 columns, you can also think of it as a row vector with length 3.
Try the following command at the FreeMat prompt.
>> length(v)
There is supposed to be a window help command, but it seems to make the program crash. For help and documentation, go back to the site: http://freemat.sourceforge.net/#help

Entering Column Vectors
Entering column vectors is also easy in FreeMat. Enter the following command at the FreeMat prompt.
>> w=[4;5;6]
Note that semicolons delimit the rows of a column vector. Try the following commands.
>> w
>> size(w)
The information supplied by the result of this last command indicates that the vector w has 3 rows and 1 column. Although you can think of the vector w as a matrix with 3 rows and 1 columns, you can also think of it as a column vector with length 3.
Try the following command at the FreeMat prompt.
>> length(w)
The Transpose Operator
You can use FreeMat’s transpose operator (a single apostrophe) to change a row vector to a column
vector.
Or a column vector to a row vector.
>> y=y’
Indexing Vectors
Once you have defined a vector, you can easily access any of its elements with FreeMat’s powerful indexing commands. For example, enter the following vector.
>> x=[10,13,19,23,27,31,39,43,51]
Now try each of the following commands.
>> x(2)
>> x(7)
You can easily change an element of a vector.
>> x(6)=100
You can also access a range of elements.
>> x([1,3,5])
>> x(1:3)
>> x(1:2:length(x))
Vector-Scalar Operations
Scalar-vector operations are straightforward. Try each of the following FreeMat commands and study the resulting output.
>> y=1:5 >> 2*y
>> y+2 >> y/2
>> y-2
Of course, these operations are equally valid when using column vectors.
>> w=(1:3:20)’ >> 0.1*w >> w+3 >> w/10 >> w-11
>> y=(1:10)’

Vector-Vector Operations
First enter the following vectors.
>> a=1:3
>> b=4:6
Adding and subtracting vectors is natural and easy. Try the followingFreeMat commands.
>> a,b,a+b
>> a,b,a-b
Because no semicolons are present to prevent suppression of output, the command a,b,a+b will display the vector a, display the vector b, then display the vector a+b. Of course, these operations are equally valid when using column vectors.
>> a=(1:3)’,b=(4:6)’
>> a+b,a-b
However, you can arrive at unexpected results if you do not remember that FreeMat is a matrix environment.
>> a,b,a*b
This last command leads to an error because * is FreeMat’s symbol for matrix multiplication and you are not using it properly.
You can also get into trouble if you attempt to add (or subtract) vectors of unequal lengths.
>> a=1:3,b=4:7,a+b
Elementwise Operations
If you wish to multiply the vectors a and b on an element-by-element basis, you enter the following FreeMat command.
>> a=1:3,b=4:6,a.*b
The symbol .* (dot-star) is FreeMat’s elementwise multiplication operator. The output is computed by multiplying the first elements of the vectors a and b, then the second elements of each vector, etc. FreeMat’s elementwise division operator, ./, works in much the same manner.
>> a=1:3,b=4:6,a./b
If you wish to exponentiate on an element-by-element basis, use .^.
>> a,a.^2
More Complicated Expressions
With a little practice you will soon learn to evaluate quite complicated expressions. Suppose, for example, that you wish to evaluate the expression x2-2x-3 for values of x ranging from 1 through 10, in increments of 1.
>> x=1:10;
>> y=x.^2-2*x-3
Take a moment to check these results with a calculator.
Let’s look at another example. Suppose that you would like to evaluate the expression (sin x)/x for values of x ranging from -1 to 1 in increments of 0.1.
>> x=-1:0.1:1;
>> y=sin(x)./x
Check these results with a calculator.
FreeMat’s elementwise operators will also work on column vectors.
>> xdata=(1:10)’
>> xdata.^2

Worksheet Assignment
Answer questions 1-9 on text editor (.txt) and save the file as “LastnameFirstinitial_m270WS1.” Submit in WebAccess. Be sure to test your responses at the computer.
1. Write the MatLab commands that generate each of the following vectors.
1 (a) [ 2 ]
−3
(b) [1, 2, –1, 3]
(c) A column vector containing all the odd numbers from 1 to 1000. (d) A row vector containing all of the even numbers from 2 to 1000.
2. If x=0:2:20, write the MatLab command that will square each entry of x.
3. If x=[0,1,4,9,16,25], write the MatLab command that will take the square root of each entry of x. 4. If x=0:0.1:1, write the MatLab command that will raise each entry of x to the power of 2/3.
5. If x=0:pi/2:2*pi, write the MatLab command that will take the cosine of each entry of x.
6. If x=-1:0.1:1, write the MatLab command that will take the inverse sine of each entry of x.
7. If x=-1:0.1:1, write the MatLab command that will take the exponential of each entry of x.
8. If x=linspace(0,2*pi,1000), what is the 50th entry of x? What is the length of x?
9. If k=0:100, what is the 12th entry of y=0.5.^k?
10. Obtain a printout of the graph resulting from the following sequence of commands.
t=linspace(0,2*pi,1000);
x=2*cos(3*t);
y=3*sin(5*t);
plot(x,y,’r’);
label(‘x-axis’,’y-axis’);
title(‘The graph of x=2*cos(3*t), y=3*sin(5*t)’);