Starting from:

$30

CS113-Exercise 1 Solved

In-Class Exercise – Command Line
Use the command line to calculate the following (some of the expected results are shown in parentheses): 

1.    Create your name as a string and assign it to the variable “Name”. 

2.    Calculate   (ans = 0.1875) 

3.    Assign variable “a” to have a value of 6 

4.    Create a variable “c” and set it equal to variable “a” times 2 (c = 12). 

5.    Create a variable “w” that is equal to: 

 

6.    Find the cube of each element in w. That is, w3

 

In-Class Exercise – MATLAB script

1.    Create a blank MATLAB script file.        


2.    Copy and paste the following into the script 

%% In-Class Exercise - Using MATLAB's Publish Feature 

% TYPE YOUR NAME AND THE DATE HERE 



% This exercise illustrates MATLAB's publish feature for a script with user 

% inputs, sections, and a simple plot 



% Note: If the first section is all comments (i.e. doesn't include any % commands), it becomes the published document's title and header. 

%% Section 1: Define Constants and Parameters clear all;close all;clc; 

a = input('input a: ') %Prompt user for value 

%a = 7 %hard coded for publishing, i.e. pretend the user input “7” 

%% Section 2: Generate Data 

% Using the data input by the user, create a set of time values 

% and calculate displacement x = 1:a %output NOT suppressed so output is included when published y = x.^3; %output IS suppressed so this won't output when published %% Section 3: Plot data 

% Plot data with title, axis labels with units, and other info plot(x,y) 

title('Membrane Displacement') 
xlabel('Time (s)') % For full credit ALWAYS include units ylabel('Displacement (m)') % Add your name to your plot text(1.5, 10,'REPLACE WITH YOUR NAME') 

3.    Run the script by pressing F5 and enter a number in the Command Window when prompted. 

4.    

 
 
 

Modify the script by following the two (2) instructions in ALL CAPS embedded in the script. 

5.    Publish a the script file to a word document by selecting “Edit 

Publishing Options” from the Publish tab, selecting “doc” as the “Output file format”, and then clicking “Publish”. 


6.    In the word file generated, find the error produced by the input statement. In the script, the error can be fixed by commenting out (i.e. add “%” in front of it) the line with the “input” statement and making the line below it active by removing the comment character “%”. Modify but do NOT delete both lines.

7.    Publish the script again after dealing with the input statement. If done correctly, there should be no errors and the command line outputs and plot should be in the document. 

8.    Compare the script file and the published file. Notice how the sections (i.e. parts of the script separate by lines starting with “%% ”) become section headers in the published document and everything output to the command line (i.e. all output not suppressed with a “;” at the end) or plotted is included just after each section. 

In-Class Exercise – Variables and Arrays
Problem 1. Use one of the built in MATLAB matrix functions to create a matrix called M12 with 5 rows and 6 columns such that has every element is equal to 12. Do NOT key in every number – i.e. [12,12,12… Problem 2. 

a.               Create a random matrix with between 2 and 10 rows and between 2 and 10 columns using “rand(randi(9)+1, randi(9)+1)”. Do NOT add a semicolon suppressing the output.

b.              Create a variable “rows” that is equal to the number of rows and “cols” that is equal to the number of columns of the matrix generated. 

c.               Assign the value in the last row and next to last column to the variable “w”. Your solution should work even if you didn’t know the number of columns and rows. That is, do NOT use the results above to do this. 

d.              Use the MATLAB publish feature to publish your script to a Word file.

Problem 3. 

Execute the following in the command window: 

a.                      Clear the command window using the clc command 

b.                      Type: % NAME HW1, Problem 3 (replace NAME with your actual name). 

c.                      Create a variable y with multiple rows and columns that has the values 

d.                      Add 16 to each element of y and assign it to the variable ra. 

e.                      Compute √𝑒𝑥 where x represents an element of y and assign it to the variable rd. Your result should be another matrix the same size as y. 

More products