{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

This preview shows pages 1–5. Sign up to view the full content.

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.

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

View Full Document
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
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 + + = - = + -

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

View Full Document
MATLAB M-File: Bungee Jumper
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}