Question
Use bisection and RK4 write matlab code
in Fnd v such that ycoo)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

(1) Bisection Method in MATLAB :

a=input('Enter function with right hand side zero:','s');

f=inline(a);

xl=input('Enter the first value of guess interval:') ;

xu=input('Enter the end value of guess interval:');

tol=input('Enter the allowed error:');

if f(xu)*f(xl)<0

else

    fprintf('The guess is incorrect! Enter new guesses\n');

    xl=input('Enter the first value of guess interval:\n') ;

    xu=input('Enter the end value of guess interval:\n');

end

for i=2:1000

xr=(xu+xl)/2;

if f(xu)*f(xr)<0

    xl=xr;

else

    xu=xr;

end

if f(xl)*f(xr)<0

    xu=xr;

else

    xl=xr;

end

xnew(1)=0;

xnew(i)=xr;

if abs((xnew(i)-xnew(i-1))/xnew(i))<tol,break,end

end

str = ['The required root of the equation is: ', num2str(xr), '']

(2) RK4 Method in MATLAB :

function a = runge_kutta(df)

% asking initial conditions

x0 = input('Enter initial value of x : ');

y0 = input ('Enter initial value of y : ');

x1 = input( 'Enter value of x at which y is to be calculated : ');

tol = input( 'Enter desired level of accuracy in the final result : ');

%choose the order of Runge-Kutta method

r = menu ( ' Which order of Runge Kutta u want to use', ...

' 2nd order ' , ' 3rd order ' , ' 4th order ');

switch r

case 1

% calculating the value of h

n =ceil( (x1-x0)/sqrt(tol));

h = ( x1 - x0)/n;

for i = 1 : n

X(1,1) = x0; Y (1,1) = y0;

k1 = h*feval( df , X(1,i), Y(1,i));

k2 = h*feval( df , X(1,i) + h , Y(1,i) + k1);

k = 1/2* ( k1+ k2);

X( 1, i+1) = X(1,i) + h;

Y( 1 ,i+1) = Y(1,i) + k;

end

case 2

% calculating the value of h

n =ceil( (x1-x0)/nthroot(tol,3));

h = ( x1 - x0)/n;

for i = 1 : n

X(1,1) = x0; Y (1,1) = y0;

k1 = h*feval( df , X(1,i), Y(1,i));

k2 = h*feval( df , X(1,i) + h/2, Y(1,i) + k1);

k3 = h*feval( df , X(1,i) + h, Y(1,i) + k2);

k = 1/6* ( k1+ 4*k2 + k3);

X( 1, i+1) = X(1,i) + h;

Y( 1 ,i+1) = Y(1,i) + k;

end

case 3

% calculating the value of h

n =ceil( (x1-x0)/nthroot(tol,3));

h = ( x1 - x0)/n;

for i = 1 : n

X(1,1) = x0; Y (1,1) = y0;

k1 = h*feval( df , X(1,i), Y(1,i));

k2 = h*feval( df , X(1,i) + h/2, Y(1,i) + k1);

k3 = h*feval( df , X(1,i) + h/2, Y(1,i) + k2);

k4 = h*feval( df , X(1,i) + h, Y(1,i) + k3);

k = 1/6* ( k1+ 2*k2 + 2*k3 + k4);

X( 1, i+1) = X(1,i) + h;

Y( 1 ,i+1) = Y(1,i) + k;

end

end

%displaying results

fprintf( 'for x = %g \n y = %g \n' , x1,Y(1,n+1))

%displaying graph

x = 1:n+1;

y = Y(1,n+1)*ones(1,n+1) - Y(1,:);

plot(x,y,'r')

xlabel = (' no of interval ');

ylabel = ( ' Error ');

Add a comment
Know the answer?
Add Answer to:
Use bisection and RK4 write matlab code in Fnd v such that ycoo) in Fnd v such that ycoo)
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