Runge-Kutta 4 with 2nd Order: Solving Variables (x,y,Q)

In summary, the conversation discusses using the Runge-Kutta 4th order method to program equations for distance, height of water, and discharge. Three different programs are mentioned, each with their own errors or limitations. The conversation also mentions the possibility of using finite difference and finite element methods, but it is determined that Runge-Kutta is the only applicable method for this system of equations.
  • #1
ahmedym_altae
1
0
i would like to programing in MATLAB with runge kutta 4 with 2nd order the variables (x with y and Q) where x distance , y hight of water and Q discharge from the following tow equation

dydx = (s0-(((Q0^2*n^2)/(B^2.*y^(10/3))*(1+2.*y/B)^(4/3))+dQdx))/(1-Q0^2/(g*B^2*y.^3));
dQdx = -2*sqrt(2)/3*ce*(y-w).^1.5*Q0/(B^2*y.^2*sqrt(g));


i traing the following program but some thing error can you correcting to me, with glad

program 1

function kutta4research2(x0,xe, y0,Q0,N)

h = (xe-x0)/N; %the step size
x(1) = x0;
y(1) = y0; %the initial value
Q(1)=Q0;
for i = 1:N
k1y = Q(i);
k1Q = h*f(x(i), y(i), Q(i));
k2y = Q(i)+k1Q*h/2;
k2Q = h*f(x(i)+h/2, y(i)+(k1y)*h/2,Q(i)+k1Q*h/2);
k3y = Q(i)+k2Q*h/2;
k3Q = h*f(x(i)+h/2, y(i)+(k2y)*h/2,Q(i)+k2Q*h/2);
k4y = Q(i)+k3Q*h;
k4Q = h*f(x(i)+h, y(i)+k3y*h,Q(i)+k3Q*h);
x(i+1)=x0+i*h;
y(i+1) = y(i) + (k1y + 2*k2y + 2*k3y + k4y)*h/6;
Q(i+1) = Q(i) + (k1Q + 2*k2Q + 2*k3Q + k4Q)*h/6;
end

[x' y' Q']

%this line of code below is optional if visualization is not needed
plot(x, y, 'r*')

function dydx = f(x, y , Q)
function dQdx = Q(i)

y0=0.17;
s0=0.0005;
Q0=0.143;
n=0.012;
B=0.15;
ce=0.785;
w=0.15;
g=9.81;
dydx = (s0-(((Q0^2*n^2)/(B^2.*y^(10/3))*(1+2.*y/B)^(4/3))+dQdx))/(1-Q0^2/(g*B^2*y.^3));
dQdx = -2*sqrt(2)/3*ce*(y-w).^1.5*Q0/(B^2*y.^2*sqrt(g));

this program error


program 2

function kutta4research3(x0,xe, y0,Q0,N)

h = (xe-x0)/N; %the step size
x(1) = x0;
y(1) = y0; %the initial value
Q(1)=Q0;
for i = 1:N
k1y = Q(i);
k1Q = h*f(x(i), y(i), Q(i));
k2y = Q(i)+k1Q*h/2;
k2Q = h*f(x(i)+h/2, y(i)+(k1y)*h/2,Q(i)+k1Q*h/2);
k3y = Q(i)+k2Q*h/2;
k3Q = h*f(x(i)+h/2, y(i)+(k2y)*h/2,Q(i)+k2Q*h/2);
k4y = Q(i)+k3Q*h;
k4Q = h*f(x(i)+h, y(i)+k3y*h,Q(i)+k3Q*h);
x(i+1)=x0+i*h;
y(i+1) = y(i) + (k1y + 2*k2y + 2*k3y + k4y)*h/6;
Q(i+1) = Q(i) + (k1Q + 2*k2Q + 2*k3Q + k4Q)*h/6;
end

[x' y' Q']

%this line of code below is optional if visualization is not needed
plot(x, y,x,Q ,'r*')

function dy = f(x, y , Q)

y0=0.17;
s0=0.0005;
Q0=0.143;
n=0.012;
B=0.15;
ce=0.785;
w=0.15;
g=9.81;
th=12;
dy = (s0-((((Q0^2*n^2)/B^2.*y^(10/3))*(1+2.*y/B)^(4/3))+2*sqrt(2)/3*ce*(y-w).^1.5*Q0/((B^2*y.^2*sqrt(g)))/(cos(th)-Q0^2/(g*B^2*y.^3))));


this program Q value constant error



program 3


function [t,x,v] = rk4ode2(func, a, b, x0, xd0, h);
% Solution of 2nd order ODE using Runge-Kutta 4th order
% with constant step size. ODE solved is converted to
% two 1st order equations. The RHS of the system is
% dv/dt = func(t, x, v)
% dx/dt = v
% See for example rhs_smd.m for forced spring-mass-damper
%
% USAGE: [t, x, v] = rk4ode2(func,a,b,x0,xd0,h)
%
% input func = name of external function to evaluate the RHS
% of the ODE (eg 'rhs_smd')
% a, b = limits of integration
% x0 = initial condition (position)
% xd0 = initial condition (velocity)
% h = stepsize
%
% output [t, x, v] = solution vectors

t = [a];
x = [x0];
v = [xd0];
i = 1;

while t(i) < b

k1x = v(i);
k1v = feval(func, t(i) , x(i) , v(i) );

k2x = v(i)+k1v*h/2;
k2v = feval(func, t(i)+h/2, x(i)+k1x*h/2 , v(i)+k1v*h/2 );

k3x = v(i)+k2v*h/2;
k3v = feval(func, t(i)+h/2, x(i)+k2x*h/2 , v(i)+k2v*h/2 );

k4x = v(i)+k3v*h;
k4v = feval(func, t(i)+h , x(i)+k3x*h , v(i)+k3v*h );

i = i+1;
t(i) = t(i-1) + h;

x(i) = x(i-1) + (k1x + 2*k2x + 2*k3x + k4x)*h/6;
v(i) = v(i-1) + (k1v + 2*k2v + 2*k3v + k4v)*h/6;

end

this program i can not know how it run


and if possible solution with finite difference and finite element
 
Physics news on Phys.org
  • #2
?No, Runge-Kutta is the only numerical method that can be used for this system of equations. Finite difference and finite element methods are not applicable here.
 

1. What is Runge-Kutta 4 with 2nd Order?

Runge-Kutta 4 with 2nd Order is a numerical method used to solve systems of differential equations. It is an improvement upon the original Runge-Kutta method, providing a more accurate and efficient solution.

2. How does Runge-Kutta 4 with 2nd Order work?

This method uses a series of intermediate calculations to estimate the value of the solution at each step. These calculations are based on the slope of the function at different points, allowing for a more precise approximation of the solution.

3. What are the variables in Runge-Kutta 4 with 2nd Order?

The variables in this method are the independent variable (x), the dependent variable (y), and the differential equation (Q). The independent and dependent variables are used to define the initial conditions of the problem, while the differential equation describes the relationship between these variables.

4. What are the advantages of using Runge-Kutta 4 with 2nd Order?

Runge-Kutta 4 with 2nd Order offers several advantages over other numerical methods, including higher accuracy and stability. It also allows for larger step sizes, reducing the number of calculations needed to reach a solution.

5. In what situations is Runge-Kutta 4 with 2nd Order commonly used?

This method is commonly used in physics, engineering, and other fields where systems of differential equations need to be solved. It is particularly useful for complex systems where other methods may struggle to provide accurate solutions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Advanced Physics Homework Help
Replies
3
Views
2K
Replies
10
Views
164
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
Replies
1
Views
9K
  • Programming and Computer Science
Replies
10
Views
12K
Replies
14
Views
4K
Back
Top