Top Banner

of 28

Matlab Lab Notes and Answers

Feb 11, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/23/2019 Matlab Lab Notes and Answers

    1/28

    APPLICATION OF DIFFERENTIAL EQUATIONS

    FALLING BODIES

    Q1:

    Rewrite the second order function in terms of y1 y! see your

    notes on th"tLet Y1=xY2=y1=dx_dtY2= d2x_dt2Then you will need to create 2 m-files a follow

    Diffential Equation Codesfunctiondh_dt=diff_fun(t,y)lo!alc mdh_dt="y(2)#c$x(2)%m-

  • 7/23/2019 Matlab Lab Notes and Answers

    2/28

    end

    Main codesfunctiondiff_mainlo!alm c m='#=*+#c=*1#otions=odeset(.elTol,1e-/)#tsan=",1

    y="1#"t,x&=ode0'(diff_fun,tsan,y,otions)heiht= x(end,1) this reresents heiht at 1seccalculate chane in heihtdh= radient (x(3,1))#h_1= dh(end)#calculate chane in timedt= diff(t)#

    t_1=dt(end)#calculate chane in 4elocity4elocity = h_1%t_1lot fiure to see motionlot(t,x(3,1),r-)#leend(h)#yla!el(heiht "m&)#xla!el(time "s&)

    end

    #o edit the $ine wei%ht "nd font wei%ht of your &$ot'$ot (t)(:1*+r,-+$inewidth+!*.$"/e$(-time0s++fontwei%ht++/o$d+*

  • 7/23/2019 Matlab Lab Notes and Answers

    3/28

    Q! is simi$"r to 21 "nd see notes on th"t

    One e)tr" %$o/"$ f"ctor "dded d

  • 7/23/2019 Matlab Lab Notes and Answers

    4/28

    Q3

    Diffential Equation Codesfunctiondx_dt=diff_fun(t,x)lo!alc m 5dx_dt="x(2)#(-c$x(2)-5$x(2)62$cos(2$t))%mend

    Main codesfunctiondiff_mainlo!alm c 5otions=odeset(7.elTol7,1e-/)#x="1#0m=1#5=2#c=8#tsan=",'"t,x&=ode0'(diff_fun,tsan,x,otions)fiure#

  • 7/23/2019 Matlab Lab Notes and Answers

    5/28

    hold on#lot(t,x(3,1),7r7)#leend(7h7)#yla!el(7h7)#xla!el(7t7)

    90

    Diffential Equation Codesfunctiondx_dt=diff_fun(t,h)lo!al. dx_dt=-.:2$s;rt(2$$h(1))#end

  • 7/23/2019 Matlab Lab Notes and Answers

    6/28

    Main codesfunctiondiff_mainlo!al. otions=odeset(7.elTol7,1e-/)#x="1.=*1#=*+#tsan=",2"t,x&=ode0'(diff_fun,tsan,x,otions)fiure#hold on#lot(t,x(3,1),7r7)#leend(7h7)#

    yla!el(7h7)#xla!el(7t7)

    9'

  • 7/23/2019 Matlab Lab Notes and Answers

    7/28

    Let , then

    function dx_dt=ode_fun8(t,x)dx_dt="0'$cos(i%8)#

    x(8)#-*+end

    otions=odeset(7.elTol7,1e-/)#x="#1#/0'sin(i%8)tsan=",1'"x,y&=ode0'(ode_fun8,tsan,x,otions)#

    fiure#hold on#lot(y(3,1),y(3,2),77)#leend(7x7,7y7)#yla!el(7y7)#xla!el(7x7)#

    Working with Data (see notes)Q1:

    (2) ?sin the o!tained olynomal model, calculate the followin

    4alue*

  • 7/23/2019 Matlab Lab Notes and Answers

    8/28

    y(*')=>

    m,4$e:

    load data1*mat_coeffs = olyfit(x,y,/)#fiurelot(x,y,7o-7)hold onyfit = oly4al(_coeffs,x)#lot(x,yfit,7r-7,7Line@idth7,2)#leend(7

  • 7/23/2019 Matlab Lab Notes and Answers

    9/28

    ="1'#1'#1'#1'#8#8#8#8

    B="11*8# 1+8+*C#1'*C# 1+2*8# 181*C# 1/02*8# 188*C#1/81Theta="'0*8#'/#1+*C#1+#/0*C#C/*C# 2*8# 2*CT="2# 11*C# 1/*8# 11*8# 80*8# 2*8# 82*C# 2+*8

    D="ones(siEe()) lo() lo(B) lo(Theta)y=lo(T)#ce=DFy#

    a=ex(ce(1))!=ce(2)c=ce(8)d=ce(0)

    1=20*C#B1=18'#T1=1''#

    T1=a$(1:!)$(B1:c)$(T1:d)

    Gnswers found3a=11*2+11!= 1*0+c= -*/2/d= -*C1+T(20*C,18',1'')=8*'1+0

  • 7/23/2019 Matlab Lab Notes and Answers

    10/28

    98

  • 7/23/2019 Matlab Lab Notes and Answers

    11/28

  • 7/23/2019 Matlab Lab Notes and Answers

    12/28

    8test!

    )!9 d"t"(:3*y!9d"t"(:

  • 7/23/2019 Matlab Lab Notes and Answers

    13/28

    x="*2'tsan=",*0"t,x&=ode0'(diff_fun,tsan,x,otions)#fiurehold onlot(t,x(3,1),7r7)#lot(t,x(3,2),77)#leend(7x17,7x27)#yla!el(7x7)#xla!el(7t7)

    Q!

    functiondx_dt=diff_fun(t,x)dx_dt="x(1)62$x(2)-x(8)#x(1)6x(8)#0$x(1)-0$x(2)-'$x(8)end

    %main codeoptions=odeset('RelTol',1e-6);x0=[0,1,1];

    tspan=[0,1];[t,x]=ode45(di!!"!#n,tspan,x0,options);

    !i$#e&old on

  • 7/23/2019 Matlab Lab Notes and Answers

    14/28

    plot(t,x(,1),'');plot(t,x(,),'$');plot(t,x(,),'*');le$end('x','+','');+la*el('x');xla*el('t')

    Q3

    functiondy_dx=diff_fun(x,y)dy_dx=x$y$y-y#end

    main codeotions=odeset(7.elTol7,1e-/)#x="1tsan=",'"t,x&=ode0'(diff_fun,tsan,x,otions)#fiurehold onlot(t,x(3,1),7r7)#yla!el(7y7)#

    xla!el(7x7)

  • 7/23/2019 Matlab Lab Notes and Answers

    15/28

    90

    functiondy_dx=diff_fun(t,x)dy_dx="x(1)-x(2)#-x(2)#x(1)$sin(x(8))end

    main codeotions=odeset(7.elTol7,1e-/)#x="1,1,1tsan=",1

    "t,x&=ode0'(diff_fun,tsan,x,otions)#fiurehold onlot(t,x(3,1),7r7)#lot(t,x(3,2),77)#lot(t,x(3,8),7!7)#leend(7x7,7y7,7E7)#yla!el(7y7)#xla!el(7x7)9'

  • 7/23/2019 Matlab Lab Notes and Answers

    16/28

    functiondy_dt=diff_fun(t,y)dy_dt="y(2)#-8$y(2)-y(1)60$ex(-2$t)-'end

    main codeotions=odeset(7.elTol7,1e-/)#

  • 7/23/2019 Matlab Lab Notes and Answers

    17/28

    x="2,-1tsan=",'"t,x&=ode0'(diff_fun,tsan,x,otions)#fiurehold onlot(t,x(3,1),7r7)#lot(t,x(3,2),77)#lot(t,x(3,8),7!7)#leend(7x7,7y7,7E7)#yla!el(7y7)#xla!el(7x7)

    9/

    Integrals

  • 7/23/2019 Matlab Lab Notes and Answers

    18/28

    IGJK H?.K Y?

  • 7/23/2019 Matlab Lab Notes and Answers

    19/28

    98OO f=(x)(x*:86sin(8$x))#OO ;uad(f,1,2)

    90OO f=(x)(ex(2$x)*$sin(x*:0-2$x))OO ;uad(f,-',')

    Derivatives!"#! $% C#E"!I&' "& MI*E+ !,E

    -&E #-M Q.

    91 GM< 2 @.J TQK HGIK @GYMethod 1//0 2345563+345731+3456689//gdiff(0)341

    //sdiff(g)341

  • 7/23/2019 Matlab Lab Notes and Answers

    20/28

    orMethod ;//0 2345563+345731+3456689//ggradient(0+341))(;@tanh(34;@>8)34;@>A;47)+09fBero(f+6)Q6 C"& $E -*ED *IE Q8

    Errors

    ;13 Lare 6 small, difference of two num!ers close toether GM