Question: Matlab!! I have such function for Runge-Kutta method (3rd order) for 2nd differential equations but the following code will only work with specific function. Could

Matlab!!

I have such function for Runge-Kutta method (3rd order) for 2nd differential equations but the following code will only work with specific function. Could you fix this code so that I can pass any 2nd differential equations as argument?? Ex:

If it is clear, I will thumb up for sure :)

===============================================

a = 0;

b = 1;

%n = input('Enter the number of intervals:');

y = [2; -2];

t = 0:h:n*h;

for i=1:n

k1 = f(t(i),y(:,i));

k2 = f(t(i)+0.5*h,y(:,i)+0.5*k1*h);

k3 = f(t(i)+h,y(:,i)-k1*h+2*k2*h);

y(:,i+1)= y(:,i)+h/6*(k1+4*k2+k3); %approximated value of the ODE using RK3

end

exact = exp(-2*t)+1;

disp(max(abs(exact-y(1,:))))

plot(t,y(1,:),'r',t,exact,'b--'),grid

xlabel('t'),ylabel('y')

legend('RK3','Exact')

========================

function fty = Ex(t,y)

Y = [0 1; -1 -exp(-3*t)]*y;

u = [0; (5-2*exp(-3*t))*exp(-2*t)+1];

fty = Y + u;

end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!