Lecture+10--Subplots+_+Roundoff+error

Lecture+10--Subplots+_+Roundoff+error - Passing Functions...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Passing Functions to M-File Example: to solve the system of ODEs * Sol: create a function rigid containing the equations function dy = rigid(t,y) dy = zeros(3,1); % a column vector dy(1) = y(2)*y(3); dy(2) = -y(1)*y(3); dy(3) = -0.51*y(1)*y(2); * Use error tolerances using the odeset command solve on a time interval [0 12] with an initial condition vector y= [0 1 1] at time 0.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Passing Functions to M-File >>options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]); >>[T,Y] = ode45( @rigid ,[0 12],[0 1 1],options); * Plotting the columns of the returned array Y vs T shows the solution >>plot(T,Y(:,1),'-',T,Y(:,2),'-.',T,Y(:,3),'.') 0 2 4 6 8 10 12 -1.5 -1 -0.5 0 0.5 1 1.5
Background image of page 2
Bungee Jumper: Euler’s Method function [x, y] = Euler( f , tspan, y0, n) % solve y' = f(x,y) with initial condition y(a) = y0 % using n steps of Euler's method; step size h = (b-a)/n % tspan = [a, b] a = tspan(1); b = tspan(2); h = (b - a) / n; x = (a+h : h : b); y(1) = y0 + h*feval(f, a, y0); for i = 2 : n y(i) = y(i-1) + h *feval( f, x(i-1), y(i-1)); end x = [ a x ]; y = [y0 y ]; function f = bungee_f( t,v) % Solve dv/dt = f for bungee jumper velocity g = 9.81; m = 68.1; cd = 0.25; f = g - cd* v^2/m; Use “ feval ” for function evaluation 2 1 1 ( , ) ( ) ( ) ( , )( ) d i i i i i i c dv g v f v t dt m v t v t f v t t t + + = - = + -
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/07/2011 for the course EGM 3344 taught by Professor Raphaelhaftka during the Spring '09 term at University of Florida.

Page1 / 15

Lecture+10--Subplots+_+Roundoff+error - Passing Functions...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online