Question

Is it possible to do this without matlab?3In modelling the velocity y of a chain slipping off a horizontal platform, the differential equation y- 10/y - y/x is deriv

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Matlab code for Euler, Improved Euler and RK4 method clear all close all %Function for which solution have to do f-e(x,y) 10.x eval-2; n=(x-eval-X)/h; % at what point we have to evaluate % Number of steps for i-1:n RK4 Steps k1-h double(f (x,y)) k2-hThe solution using Euler Method for h-0.050 at x(2.0) is 3.492230 The solution using improved Euler Method for h=0.050 at x (

%%Matlab code for Euler, Improved Euler and RK4 method
clear all
close all
%Function for which solution have to do
f=@(x,y) 10./y-y./x;
hh=[0.05 0.025];

for ii=1:2
    %Euler method
    h=hh(ii);         % amount of intervals
    x=1;             % initial x
    y=1;             % initial y
    x_eval=2;        % at what point we have to evaluate
    n=(x_eval-x)/h; % Number of steps
    x2(1)=x;
    y2(1)=y;
    for i=1:n
        %Eular Steps
        m=double(f(x,y));
        x=x+h;
        y=y+h*m;
        x2(i+1)=x;
        y2(i+1)=y;
    end
    yy(ii,1)=y2(end);
    fprintf('\n\tThe solution using Euler Method for h=%.3f at x(%.1f) is %f\n',h,x2(end),y2(end))

    %Improved Euler method
    x=1;             % initial x
    y=1;             % initial y
    x_eval=2;        % at what point we have to evaluate
    n=(x_eval-x)/h; % step size
    x3(1)=x;
    y3(1)=y;
    for i=1:n
    %improved Euler steps
       m1=double(f(x,y));
       m2=double(f((x+h),(y+h*m1)));
       y=y+double(h*((m1+m2)/2));
       x=x+h;
       y3(i+1)=y;
       x3(i+1)=x;
    end
    yy(ii,2)=y3(end);
  
    fprintf('\tThe solution using improved Euler Method for h=%.3f at x(%.1f) is %f\n',h,x3(end),y3(end))
  
    %RK4 method
    x=1;             % initial x
    y=1;             % initial y
    x_eval=2;        % at what point we have to evaluate
    n=(x_eval-x)/h; % Number of steps
    x4(1)=x;
    y4(1)=y;
    for i=1:n
    %RK4 Steps
       k1=h*double(f(x,y));
       k2=h*double(f((x+h/2),(y+k1/2)));
       k3=h*double(f((x+h/2),(y+k2/2)));
       k4=h*double(f((x+h),(y+k3)));
       dx=(1/6)*(k1+2*k2+2*k3+k4);
       x=x+h;
       y=y+dx;
       x4(i+1)=x;
       y4(i+1)=y;

    end
    yy(ii,3)=y4(end);
    fprintf('\tThe solution using Runge Kutta 4 for h=%.3f at x(%.1f) is %f\n',h,x4(end),y4(end))

end
%exact solution
syms y(x)
eqn = diff(y,x) == 10/y-y/x;
cond = y(1) == 1;
y_ext(x)=dsolve(eqn,cond);

fprintf('\n\tExact solution for y(2) at x=2 is %f.\n',y_ext(2))

%Table for absolute error
fprintf('\n\n\t Absolute error of Approximations to y(2)\n')
fprintf('\tMethod        \th=0.005\th=0.025\tRatio\n\n')
v1=abs(y_ext(2)-yy(1,1)); v2=abs(y_ext(2)-yy(2,1));
fprintf('\tEuler         \t%e\t%e\t%f\n',v1,v2,v1/v2)
v1=abs(y_ext(2)-yy(1,2)); v2=abs(y_ext(2)-yy(2,2));
fprintf('\tImproved Euler\t%e\t%e\t%f\n',v1,v2,v1/v2)
v1=abs(y_ext(2)-yy(1,3)); v2=abs(y_ext(2)-yy(2,3));
fprintf('\tRunge kutta   \t%e\t%e\t%f\n',v1,v2,v1/v2)


%%Plotting solution using Euler method
figure(1)
hold on
plot(x2,y2,'Linewidth',2)
plot(x3,y3,'Linewidth',2)
plot(x4,y4,'Linewidth',2)
xlabel('x')
ylabel('y(x)')
title('Solution plot y vs. x')
legend('Euler Method','Modified Euler','RK4 Method','Location','northwest')
grid on


%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
Is it possible to do this without matlab? 3In modelling the velocity y of a chain slipping off a horizontal platform, th...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT