Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Assignment of Matlab, Assignments of Matlab skills

Assignment include 3 part for begginner student

Typology: Assignments

2022/2023

Uploaded on 06/24/2023

saladdin-ahmed-amin
saladdin-ahmed-amin 🇬🇧

1 document

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
HARAMAYAINSTITUTEOFTECHNOLOGY
SchoolofWaterResourcesandEnvironmentalEngineering
DepartmentofEngineeringHydrology
NumericalMethods&ComputerProgrammingCourse
Assignment#1,#2,#3,#4,and#5
Prepared by:-SaladinAhmedAmin IDNo:-PGP/915/15
Submittedto:-Mr.GirmaBerhe(MSc.)
Jan13,2023GC.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Assignment of Matlab and more Assignments Matlab skills in PDF only on Docsity!

HARAMAYAINSTITUTEOFTECHNOLOGY

SchoolofWaterResourcesandEnvironmentalEngineering

DepartmentofEngineeringHydrology

NumericalMethods&ComputerProgrammingCourse

Assignment# 1 ,# 2 ,# 3 ,# 4 ,and# 5

Prepared by:-SaladinAhmedAmin IDNo:-PGP/ 915 / 15

Submittedto:-Mr.GirmaBerhe(MSc.)

Jan1 3 , 202 3GC.

HaramayaUniversity,Ethiopia

Chapter 1 : Exercise 1 :Scalarquantitiesandvariables Answer: a= 2 ; b= 3 ; y=a+ 1 y= 3 x=a+b x= 5 c= 4 ; a*(b+c) ans= 14 a/(b+c) ans=

  1. 2857 a/b+c ans=
  2. 6667 Exercise 2 :Simpleuseofbrackets formatrat %towritearationalfraction Answer: clearall closeall a= 2 ; b= 3 ; c= 4 ; x=a*(b+c); formatrat x

Answer: a= 1 : 4 ; b= 2 : 4 ; disp(a) 1 2 3 4 isempty(a) ans= 0 isequal(a,b) ans= 0 isinteger(b) ans= 0 isinteger(int 8 (b)) ans= 1 isvector(a) ans= 1 isscalar(a) ans= 0 issparse(a) ans= 0 size(a) ans=

Exercise 6 :Calculatethefollowingexpressions formatshortG x=sin(pi/ 3 ); formatshortG x x=

  1. 86603 y=exp(log( 4 )); formatshortG y y= 4 z=cos(pi/ 2 ); formatshortG z z=
  2. 1232 e- 17 m=log(exp( 2 +cos(pi))); formatshortG m m = 1 n=tan(pi/ 6 )/(tan(pi/ 4 )+tan(pi/ 3 )); formatshortG n n=
  3. 21132 Practice: 1 checkthissequenceofstatementsinmatlabwindow mynum= 3

b) 2 *sin( 1. 4 ) ans=

  1. 9709 c)Formatingdecimals x= 2 *sin( 1. 4 ); formatlong x x=
  2. 970899459976920 formatshort x x=
  3. 9709 d) nthroot( 64 , 3 ) ans= 4 e)Random numbers rand ans=
  4. 8147 rand ans=
  5. 9058 f)GeneratingRandom Integers round(rand* 10 ) ans= 1

randi([ 3 , 6 ]) ans= 6 randi([ 3 , 6 ]) ans= 5 randi([ 3 , 6 ]) ans= 3 randi([ 3 , 6 ]) ans= 4 PRACTICE1. 2 .Generatearandom

  • realnumberintherange( 0 , 1 ) round(rand* 1 ) ans= 1 Realnumberintherange( 0 , 100 ) randi([ 0 , 100 ]) ans= 96 Realnumberintherange( 20 , 35 ) randi([ 20 , 35 ]) ans= 27 Realnumberintherange( 1 , 100 ) randi([ 1 , 100 ]) ans= 81 Realnumberintherange( 20 , 35 )

xor('c'=='d'- 1 , 2 < 4 ) ans= 0 10 > 5 > 2 ans= 0 CH- 2 Practicethefollowingquestions 1 .Whathappensifaddingthestepvaluewouldgobeyondtherangespecifiedbythelast,forexample 1 : 2 : 6. Answer:

pvec= 1 : 2 : 6. pvec= 1 3 5 2 .Howcanyouusethecolonoperatortogeneratethevectorshownbelow? Answer: pvec= 9 :- 2 : 1 pvec= 9 7 5 3 1 Practice 2. 1 2 .Thinkaboutwhatwouldbeproducedbythefollowingsequenceofstatementsandexpressions,and thentypethem intoverifyyouranswers: Answer: pvec= 3 : 2 : 10 pvec= 3 5 7 9 pvec( 2 )= 15 pvec=

pvec( 7 )= 33 pvec= 3 1 5 7 9 0 0 3 3 pvec([ 2 : 47 ]) ans= 15 7 9 3 3 linspace( 5 , 11 , 3 ) ans= 5 8 1 1 logspace( 2 , 4 , 3 ) ans= 100 1000 10000 Practice 2. 2 3 .Thinkaboutwhatwouldbeproducedbythefollowingsequenceofstatementsandexpressions,and thentypethem intoverifyyouranswers. Answer: mat=[ 1 : 3 ; 3 : 5 ; 5 : 7 ] mat= 1 2 3 3 4 5 5 6 7 zeros(size(mat)) ans= 0 0 0 0 0 0 0 0 0

numel(mat) ans= 12 v=mat( 3 ,:) v= 5 4 3 3 3 v(v( 2 )) ans= 33 v( 1 )=[] v= 4 3 3 3 reshape(mat, 2 , 6 ) ans= 1 5 9 3 3 1 1 44 2 4 2 8 3 3 Chapter 3 :Looping %%A)fact= 1 ; fori= 2 : 6 fact=fact*i end fact= 2 fact= 6 fact=

fact= 120 fact= 720 clearall closeall %%functionrunsum =sumnnums(n) runsum =2; n= 1 : 10 ; fori=1:n; inputnum =input('Enteranumber:'); runsum =runsum+inputnum end end Answer Enteranumber: 3 runsum = 5 ans= 5 clearall closeall %%total= 0 ; forn= 1 : 6 ; total=total+ 2 ^n; end Answer: 126 total= 0 ; fori= 3 : 7 ; total=total+i^ 3 end total= 775 %% %%maxN=input('Enterthemaximum valueofNrequired:'); I( 1 )= 1 ^ 2 ; forN= 2 :maxN I(N)=I(N- 1 )+N^ 2 ; end

x=input('plaseenterx:'); ifx>= 0 &&x<= 1 f=x elseifx> 1 &&x<= 2 f= 2 - x else f= 0 end Ans: plaseenterx: 1. 5 f=

  1. 5000 plaseenterx: 0. 5 f=
  2. 5000 Chapter4RootFinding Rootfindingbyplotinggraph Example#1Plotthegraphandidentifythezero’svalues f(x)=x-a 1 *sin(x.^a 2 );a1=2;a2=2;andx=- 2 : 0. 01 : 2 ;plot(x,y) Code: function[value]=userfn(x,a 1 ,a 2 ); a1=2; a2=2; x=- 2 : 0. 01 : 2 ; value=x-a 1 *sin(x.^a 2 ); y=value plot(x,y) gridon end Output: Thezeroisnear 0 , 0 .5andanothernear 1. 5

Example# 2 Plotthegraphandidentifythezero’svalues f(x)=sin( 10 x)+cos( 3 x) code: x=linspace( 3 , 6 ); y=sin( 10 .x)+cos( 3 .x); plot(x,y) gridon Output: Thezeroisnear 3. 25 , 3. 4 , 3. 75 , 4. 25 , 4. 70 , 5. 2 , 5. 23 ,andanothernear 5. 68 ExamplesonFindingrootsofpolynomials Example# 1

gridon end Example#2Determinetheinitialestimatesforthezerosofthefunctionf(x)=xsinx–x^ 0. 5 code: =@(x)x.*sin(x)-x.^ 0. 5 ; fzero(f, 0 ) output: ans= 0 ExamplesonFixedPointIterationmethod Example# 1 FixedpointIterationmethodtofindingroots f(x)=2-x+ln(x) x=e^(x- 2 )xn+1=2+ln(x) TosolvetherootbyMs-excelasfollows; Iteration X=e^(x- 2 )xn+1=2+ln(x) 1 1 1 2 0. 3678794412 3 0. 1955145342. 693147181 4 0. 1645591062. 990710465 5 0. 1595431453. 095510973 6 0. 1587448863. 129952989 7 0. 1586182173. 141017985 8 0. 1585981263. 144546946 9 0. 158594943. 145669825

    1. 146026848
    1. 146140339
    1. 146176412
    1. 146187878
    1. 146191523 SolvebyusingMatlabasfollows; code:forx=e^(x- 2 ) 1 stsolution: x0=0; maxIter=5 0 ; tolx=0. 0001 ; x=x 0 ; xold=x 0 ; fori=1:maxIter x=exp(x- 2 ); err=abs(x-xold); xold=x; if(err<tolx) break; end end Output: 0. 158 6isanswer

2 ndSolution: code:forx=2+sin(x) Example#2 FixedpointIterationmethodtofindingroots f(x)=e^x-x xn+1=e^xn Iteration X 1 0 2 1 3 0. 367879441 4 0. 692200628 5 0. 500473501 6 0. 606243535 7 0. 545395786 8 0. 579612336 9 0. 560115461

  1. 571143115
  2. 564879347
  3. 568428725
  4. 566414733
  5. 567556637
  6. 566908912
  7. 567276232
  8. 567067898
  9. 5671860 5xr=0. 5671 6after 2 0iteration
  10. 5671190 4f(xr) =- 0. 000022 =
  11. 567157044 SolvebyusingMatlabasfollows; code: Output: x0= 10 ; x=0. 5671 2isthefinalanswer maxIter=5 0 ; tolx=0. 0001 ; x=x 0 ; xold=x 0 ; fori=1:maxIter x=exp(-x); err=abs(x-xold); xold=x; if(err<tolx) break; end end otherCode: clear;clc;format('long','g') i=1; x(i)=0; error(i)=9 999 ; whileerror(i)>=0. 1