Skip to content
Permalink
0908032904
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
49 lines (44 sloc) 927 Bytes

06_initial_value_ode

Problem 2

Part A

The analytical solution to y' = -y is y = e^-t

Part B

dt=[0.5 .1 .001];
figure();
hold on
     x=[0:dt(2):3]';
     y_n=zeros(size(x));
    y_n(1)=1;
for i=2:length(x)
    dy=prob2_ode(x,y_n(i-1));
    y_n(i)=y_n(i-1)+ dt(2)*dy;
end  
plot(x,exp(-x),'k-',x,y_n,'o');

Part C

function y=Prob2ptC(ode,dt,y0,tlimits)
maxit=100;
t=[tlimits(1):dt:tlimits(2)];
y=zeros(length(t),1);
y(1)=y0;
    for i=2:length(t)
        dy=ode(t(i-1),y(i-1));
        y(i)=y(i-1)+dt*dy;
        dyc=(dy+ode(t(i),y(i)))/2;
        y(i)=y(i-1)+dt*dyc;
        nits=1;

        while(1)
            nits=nits+1;
            yold=y(i);
            dyc=(dy+ode(t(i),y(i)))/2;
            y(i)=y(i-1)+dt*dyc;
            ea=abs(yold-y(i))/y(i)*100;
            if ea<.0001|nits>maxit;break;end;
        end;
    end
end