You are on page 1of 16

Wavez 2011-2012

Accepting Loop

user inputs (input function)

Functions (for and while loops) Equations

Simultaneous Differential 3-D

Equations

Plot Functions (surf and ezsurf)

We

use the input() function for accepting data from the user

Syntax:

<var_name> = input(<prompt string>) Eg: n = input(Enter a number: );

Loops are commonly used for: Initialisation of large matrices


Carrying

out repeated tasks a fixed number of times your system to crash

Causing

All loops are divided into 4 main sections:


1. 2. 3. 4.

Initialisation Execution Updation Loop Exit

Syntax:
for <array> loop body end

Note that you can also create the array as youre starting the loop

Example

code for calculating Factorial:

f = 1; n = input(Enter number: ); for i=1:n f=f*i; end disp(The factorial is); disp(f);

Syntax:
while (<condition>) loop body variable iteration end

Same

example for finding factorial:

f = 1; n = input(Enter a number: ); while(n>=1) f=f*n; n=n-1; end disp(Your answer is); disp(f);

A simultaneous equation is of the form: a1x + b1y = c1 a2x + b2y = c2 This can be written in matrix form as: AX = B Where A=[ a1,b1 ; a2,b2 ] , X=[ x ; y ] B=[ c1 ; c2 ]

This equation can be solved by taking: AX = B => X = A-1B So, our solution is:
X = inv(A)*B;

Refer

to Matlab Workshop III for details.

Apart

from the plot() function, for a 2D plot, we also have functions for plotting 3D surfaces. is done by the surf() and ezsurf() functions

This

Syntax:
surf( <2D array> ); OR surf(<1D x array>,<1D y array>,<2D array>);

Syntax:
syms <variable list>; ezsurf( <function> ); OR ezsurf(<function>,[<-xlim> <xlim> <ylim> <ylim>]);

Try plotting the graph for 3x^2 + 1/y^2 :D

Thats All Folks!

You might also like