SOLUTION FOR MODEL PAPER2
SOLUTION FOR MODEL PAPER2
Module -1:
1. a. With neat diagram explain MATLAB.
Type >> area= pi*(pi^ (1/3)-1) ^2 in MATLAB command window and press enter.
The output after execution is,
area =0.6781 .Thus, the solution for the arithmetic operation is 0.6781.
b. Explain the steps involved in creating and executing a function file. (5M)
Ans: A function file is also an M-file, just like a script file, except it has a function definition
line on the top that defines the input and output explicitly.
How to create and execute a function file:
Method : Example: Write a function file to draw a circle of a specified radius, with the
radius as the input to the function.
1. Open the script file circle. m:
• On PCs: Select File---->Open ... from the File menu. Navigate and select the file circle.m from the
Open dialog box. Double-click to open the file. The contents of the file should appear in an edit
window.
2. Edit the file circle .m
function [x , y] = circlefn (r) ;
% CIRCLEFN - Function to draw a circle of radius r.
% File written by Rudra Pratap on 9/ 17/94. Last modified 7/ 1 /98
% Call syntax : [x , y] = circlefn (r) ; or just : circlefn (r) ;
% Input : r = specified radius
% Output : [x , y] = the x- and y-coordinates of data points
theta=linspace ( 0 , 2*pi , 100) ; % create vector theta
x = r*cos (theta); % generate x-coordinates
y = r*sin (theta) ; % generate y-coordinates
plot (x , y) ; % plot the circle
axis ( ' equal') ; % set equal scale on axes
title ( [ ' Circle of radius r = ' , num2str (r) ] ) % put a title with the value of r
Alternatively, you could select File---->New---->Function M-File and type all the
lines in the new file.
3. Now write and save the file under the name circlefn.m as follows:
• On PCs: Select Save As ... from the File menu. A dialog box should appear. Type circlefn .m as the
name of the document. Make sure the file is saved in the folder you want (the current working
folder/ directory of MATLAB). Click Save to save the file.
Executing a function file:
>> R = 5; % Specify the input and execute the function with an explicit output list.
>> [x , y] = circlefn (R) ;
>> [ cx, cy ] = circlefn ( 2.5 ) ; % You can also specify the value of the input directly.
>> circlefn ( 1) ; % If you don't need the output, you don't have to specify it.
>> circlefn ( R^2 / (R+5* sin (R) )); %Of course, the input can also be a valid MATLAB
expression.
Note: 1. A function file (see previous page) must begin with a function definition line.
2. The argument of the title command in this function file consists of two character strings. The first
one is a simple character string, ' Circle of radius r ='. The second one, num2str (r), is a function that
converts the numeric value of r to a string (and hence the nameof the function) . The square brackets
create an array of the two strings by concatenating them.
b. Explain how to use publisher from the editor window with example. (5M)
Ans: MATLAB includes an automatic report generator called publisher.
This publisher is accessible, like many other utilities, both from the menu and
the command line. The publisher publishes a script in several formats, including
HTML, XML, MS Word, PowerPoint, etc.
Using the publisher from the editor window:
Open the file circle.m .You can open the file by following File ---> Open from the menu or from
the command window with either circle .m or open circle .m . Once the file is open in the editor,
select Publish from the File menu of the editor window, or click on the publish icon icon (next to
the printer icon in the menubar) . The script open in the editor will be published to an HTML file
and MATLAB will automatically open the HTML file for your perusal.
6. a. Write a MATLAB code to solve the following set of simultaneous linear algebraic
equations. x+ 3y –z=2 (5M)
x-y+z=3
3x - 5y =4.
Ans: First initialize the symbolic variables x , y, z . And then write the appropriate code to represent
expressions.
>> syms x y z
>> exp1 = ‘ x + 3 * y - z – 2’ % Solve three simultaneous algebraic equations for x and y and
z
>> exp 3 = ‘ 3 * x – 5 * y - 4’
b. Explain three different kinds of files for reading data into MATLAB's workspace
Module-4:
Variable Names
Valid Names:
Avoid creating variables with the same name as a function (such as i, j, mode,
char, size, and path). In general, variable names take precedence over function
names. If you create a variable that uses the name of a function, you sometimes
get unexpected results.
How to name variables: Names of variables must begin with a letter. After the first letter, any number
of digits or underscores may be used, but MATLAB remembers only the. first 31 characters.
What is the precision of computation: All computations are carried out internally in double precision
unless specified otherwise. The appearance of numbers on the screen, however, depends on the format
in use. The output appearance of floating-point numbers (number of digits after the decimal, etc.) is
controlled with the format command. The default is format short , which displays four
digits after the decimal.
How to recall previously typed commands: Use the up-arrow key to recall previously typed
commands. MATLAB uses smart recall, so you can also type one or two letters of your command and
use the up-arrow key for recalling the command starting with those letters. Also, all your commands
are recorded in the command history subwindow. You can double-click on any command in the
command history subwindow to execute it inthe command window.
Module-5 :
9. a Explain script files with example.
Ans: Script Files
A script file is an M-file with a set of valid MATLAB commands in it. A script file is
executed by typing the name of the file (without the .m extension) on the command
line. It is equivalent to typing all the commands stored in the script file, one by
one , at the MATLAB prompt. Naturally, script files work on global variables, that
is, variables currently present in the workspace. Results obtained from executing
script files are left in the workspace. Script files are useful when you have
to repeat a set of commands several times.
Example of a script file:
Let us write a script file to solve the following system of linear equations:
5 2r r x1 2
3 6 2r-1 x2 = 3
2 r-1 3r x3 5
This time, we will make T an input to the function and det_A and x will be the output.
Let us call this function solvexf . As a rule, it must be saved in a file called solvexf. m.
function [det _A , x] = solvexf (r) ;
% SOLVEXF solves a 3X3 matrix equation with parameter r
% This is the funct ion f ile ' solvexf .m'
% To call this funct ion , type :
% [det_A , x] = solvexf (r) ;
% r is the input and det_A and x are output .
% ______________________________ ______________________ _
A [5 2*r r; 3 6 2*r- 1 ; 2 r-1 3*r] ; % create matrix A
b= [2 ; 3 ; 5] ; % create vector b
det_A = det (A); % find the determinant
X = A\b ; % find x
Now r, x, and det_A are all local variables. Therefore, any other variable names
may be used in their places in the function call statement. Let us execute this
function in MATLAB.
>> clear all
>> [det A , y] = solvefx(1) % take r =1 and solvexf.m
>> det A
det A
= 64
>> y % display the value of y
Y=
-0.0312
0.2344
1.6875 % Values of detA and y will be automatically displayed if the semi-
colon at the end of the function command is omitted.
>> who % Note that only detA and y are in the workspace; no A, b, or x.
Your variables are
det A y
After execution of a function, the only variables left in the workspace by the
function will be the variables in the output list. This gives us more control over
input and output than we can get with script files.
• [radius , h] =mot ion (rx , ry) ; Call with partial list of input and out-put . The third input
variable must be assigned 11 default value inside the function if it is required in calculations.
The output corresponds to the first two elements of the output list of motion.
2. Without any output: The output list can be omitted entirely if the computed quantities are
not of any interest. This might be the case when the function displays the desired result
graphically. To execute the function this way, just type the name of the function with the
input list. For example,motion (xt , yt , time ) ; will execute the function mot ion without
generating any explicit output, provided that xt, yt, and time are defined. If the semi-colon at
the end of the call statement is omitted, the first output variable in the output list of the
function is displayed in the default variable ans.