MatLab Help

© All Rights Reserved

15 views

Matlab Lecture 9

MatLab Help

© All Rights Reserved

- Chapter6_01
- Beer Solutions Chapter 9
- Beer Solutions
- Qualitative Analysis of Cations
- STA CH07 Lecture
- The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
- Sapiens: A Brief History of Humankind
- Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
- Shoe Dog: A Memoir by the Creator of Nike
- The Unwinding: An Inner History of the New America
- The Little Book of Hygge: Danish Secrets to Happy Living
- Never Split the Difference: Negotiating As If Your Life Depended On It
- Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
- Yes Please
- A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
- Grit: The Power of Passion and Perseverance
- Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
- The Emperor of All Maladies: A Biography of Cancer
- John Adams
- This Changes Everything: Capitalism vs. The Climate

You are on page 1of 9

Programming in MATLAB

FOR LOOP

N = input(Enter a positive integer:);

fact = 1;

for k = 1:N

fact = fact * k;

end

fprintf(The factorial of %4.0f is %10.0f \n, N, fact);

Lets deal with a geometric series G = pk where

p = 0.99 and k = 0 to 1000

Calculate each term in the series (before

summation) geomSeries = pk

Calculate the value of the infinite series

G = pk = 1 /(1-p) = 1/(1-.99) = 100

G = 0;

p = 0.99;

for k = 0:1000

geomSeries = p^k ;

G = G + geomSeries ;

plot(k, geomSeries, 'o'); hold on;

end

disp(G);

p = 0.99;

k = 0:1000;

geomSeries = p .^k

plot(k, geomSeries);

G = sum(geomSeries);

fprintf('Sum of geometric Series is %f \n', G);

MATRICES

% matix dimension should be the same for both

A = [ 1 4 8; 5 3 -1 ; 0 7 9; 4 3 2] ; %4 rows, 3 columns

B = [2 3 4; 5 6 7; 8 9 1; 0 2 3] ;

% i is the row number , j = column number

for i = 1:4

for j = 1:3

C(i,j) = A(i,j) + B(i,j) ;

end

end

%display C

disp(C);

% matix dimension should be the same for both

A = [ 1 4 8; 5 3 -1 ; 0 7 9; 4 3 2] %4 rows, 3 columns

B = [2 3 4; 5 6 7; 8 9 1; 0 2 3]

C =A+ B

CONSTRUCT AN IDENTITY MATRIX

%Identity matrix of size 3x3

for i = 1:3

for j = 1:3

if i == j

ID(i,j) = 1;

else

ID(i,j) = 0;

end

end

end

disp(ID)

IMPORT/EXPORT DATA

You can open any file from MATLAB by using

fopen() command

Use help fopen to find more

fid = fopen(filename, r) open file for reading

fid = fopen(filename, w) open file for writing

fclose(fid) Close the file.

fprintf() to write data on the file

fscanf() to read data from the file

FOPEN()

fid=fopen('C:\Users\student\D

ocuments\MATLAB\Example.t

xt', 'w');

for i = 1:20

new = [i, 10*rand(1,2)];

fprintf(fid, '%f %f %f\n',

new);

end

fclose(fid);

data = fscanf(fid,'%f %f %f',

[3,inf])';

% '3' for three columns, 'inf' means

up to the last column

fclose(fid);

% data contains 3 columns and 20

rows

plot(data(:,1), data(:,2));