(Complete all the tasks and publish in html, and send the html to the email, Due Monday next week)
Task 1: Before
we move on to the Data input/output part of Matlab, we have a leftover
from last lecture: 1) Solve for c1 and c2. 2) Plot the scattered data
points (use t=[0 0.3 0.8 1.1 1.6 2.3];). 3) Create a new 't'
vectore (about 40 points), plot a smooth line of this function on the
same plot. (refer to the online tutorial on Monday if you need).
Note that, if the coefficient matrix is not square, you can't use inv(A), you can use x=A\b to solve it instead.
Be sure you use '\' but not '/' for this.
The final plot should look like this:
An example code for this:
A=[1 exp(0); 1 exp(-0.3); 1 exp(-0.8); 1 exp(-1.1); 1 exp(-1.6); 1 exp(-2.3)];
c=A\b % when you can't use inv(A), just use this '\' to get it.
t_scatter=[0 0.3 0.8 1.1 1.6 2.3]
y_scatter=[0.82 0.72 0.63 0.60 0.55 0.50]
Task 2: Data read and write:
Download this .csv data file: link
These two commands will read a .csv file, load it to M; then create a new .csv file, write M into it.
Do the same thing to an .xlsx file: link
Task 3: Display a table: to display a table like the following in the command window:
can you control the space/layout of the table? Well, this example below is
not the optimal method to do this job, but look at the following code:
You need to leave a bunch of 'space' to layout the table correctly. Anyway, complete this task for credit before you move on.
Task 4: Use 'fprintf to display text':
** (What does 'fprintf' stand for? - File Print Format, print a file in a certain format)
Include this in your script:
To show numbers, you will need a different format:
and More: (these slides are very important, if you forget the
format in fprintf in the future, just come back to the website to check
Now, it is the time to practice:
Include the following code in your script. The
reason for two '' to make Mom's displayed is Matlab needs to know you
are trying to make an 'apostrophe' but not trying end this string.....
You don't need the bracket actually: try this:
fprintf('Tim''s weight is %.1f, age is %.1f', weight, age)
Task 5: More examples about fprintf: \t means a column break. It will starts a new column in a table.
T as: T=[1 2 3 4 5 6 7 8] (they are just random numbers, ignore the
sqrt there), you will find out the result in the table will be laid out
in the order of 'top to bottom, left to right'. It is hard to explain
here, try these different T, and look at the elements in the table.
T=[1 2 3 4 5 6 7 8]
T=[1 2; 3 4; 5 6; 7 8]
T=[1 2 3; 3 4 5; 5 6 7; 7 8 9]
now you know the order that fprintf follows to extract the numbers from
the vector. To correctly show the correct results above, you need this:
T=[1 1 2 1.41421 3 1.732 4 2 5 2.236]
Task 6: Write data to a .txt file. (useful)
purpose of doing this is to write the data you have in matlab to a .txt
file. So it is easy to be transferred and copied, or reloaded by other
software in the future.
will see a .txt file created in the same folder as your script file.
Change the x range to 1 - 30, with 10 points (use linspace()), and then
store it in a .txt file named OMG.txt, attach it to your homework