Defining Differential Equation in Runge Kutta 4th order

3 views (last 30 days)
Hello!
I am trying to solve the following differential equation with runge-kutta, 4th order but I have no idea how to define the equation in matlab :/
What I have so far is:
%functional terms
Pel= rho*l*I^(2)/Aw;
Pc = h*Al*(T-Ta);
Pe = boltzmann*e*al*(T^(4)-Ta^(4));
%Differential equation
h=0.1; a=0; b=100; % h is the step size, t=[a,b] t-range
t = a:h:b; % Computes t-array up to t=100
T = zeros(1,numel(t)); % Memory preallocation
T(0) = Ta; % initial condition; in MATLAB indices start at Ta
T_dot =@(T)(delta/(m*ct))*Pel(T)-(Pc(T)+Pe(T)); %insert function to be solved
% the function is the expression after (T,t)
% table title
fprintf(fid,'%7s %7s %7s %7s %7s %7s %7s \n','i','t(i)','k1','k2','k3',
'k4','y(i)');
for ii=1:1:numel(t)
k1 = T_dot(t(ii),T(ii));
k2 = T_dot(t(ii)+0.5*h,T(ii)+0.5*h*k1);
k3 = T_dot((t(ii)+0.5*h),(T(ii)+0.5*h*k2));
k4 = T_dot(t(ii)+h),(T(ii)+h*k3));
T(ii+1) = T(ii) + (h/6)*(k1+2*k2+2*k3+k4); % main equation
% table data
fprintf(fid,'%7d %7.2f %7.3f %7.3f',ii, t(ii), k1, k2);
fprintf(fid,' %7.3f %7.3f %7.3f \n', k3, k4, T(ii));
end
T(numel(t))=[ ]; % erase the last computation of T(n+1)
% Solution PLOT:
plot(t,T,' ok')
title('RK-4--Numerical Solution---');
ylabel('T'); xlabel('t'); legend('numerical');
grid on
fclose(fid);
Honestly, I have copied the code from a site and tried to adapt it to my equation, but as I am a complete beginner I have no idea where or what I did wrong, I just know that it's wrong.
Maybe someone can help me out? I'd appreciate every help :)!
  2 Comments
Torsten
Torsten on 12 Jul 2022
We need rho, l, I, Aw, h, Ta, boltzmann, e, al, delta, m, ct.
And your function definition is not the same as in your mathematical formula.
phkstudent
phkstudent on 12 Jul 2022
I defined those parameters before the given code, but I left it out cause I thought it might look cinfusing and too much
and yes, I have noticed that I solved my equation incorrectly (though Pw is left out on purpose)
Ta = 300;
g = 9.81;
V=12; %Volt
I=10; %Ampere
delta = 1; %switch
r=0.01; %[m]
l=0.2; %[m]
Aw = pi*r^2;
Al = l*2*r;
m=6500*Aw*l;
e=0.37;
ct= 470;
boltzmann = 5.67*10^(-8); %[W/m^2K^4]
kinvis= 15.89;
dynvis= 184.6*10^(-13);
cp= 1.007; %specific heat
kg= 26.3^(-6); %thermal conductivity gas
Pr = dynvis*cp/kg;
Gr = (2*(T-Ta)/(T+Ta))*g*r^(3)/(8*kinvis^(2));
Nu = (1+0.287*((Gr*Pr)/(1+(0.56/Pr)^(9/16))^(9/16)))^(2); %Nusselt
Kg = 0.026;
rho = V*Aw/(I*l);
h=Nu*kg/l; %thermal conductivity coefficent

Sign in to comment.

Accepted Answer

Torsten
Torsten on 12 Jul 2022
Ta = 300;
g = 9.81;
V=12; %Volt
I=10; %Ampere
delta = 1; %switch
r=0.01; %[m]
l=0.2; %[m]
Aw = pi*r^2;
Al = l*2*r;
m=6500*Aw*l;
e=0.37;
ct= 470;
boltzmann = 5.67*10^(-8); %[W/m^2K^4]
kinvis= 15.89;
dynvis= 184.6*10^(-13);
cp= 1.007; %specific heat
kg= 26.3^(-6); %thermal conductivity gas
Pr = dynvis*cp/kg;
Gr = @(T)(2*(T-Ta)/(T+Ta))*g*r^(3)/(8*kinvis^(2));
Nu = @(T)(1+0.287*((Gr(T)*Pr)/(1+(0.56/Pr)^(9/16))^(9/16)))^(2); %Nusselt
Kg = 0.026;
rho = V*Aw/(I*l);
h=@(T)Nu(T)*kg/l; %thermal conductivity coefficent
%functional terms
Pel= @(T)rho*l*I^(2)/Aw;
Pc = @(T)h(T)*Al*(T-Ta);
Pe = @(T)boltzmann*e*Al*(T^4-Ta^4);
Pw = @(T)0;
%Differential equation
h=0.1; a=0; b=100; % h is the step size, t=[a,b] t-range
t = a:h:b; % Computes t-array up to t=100
T = zeros(1,numel(t)); % Memory preallocation
T(1) = Ta; % initial condition; in MATLAB indices start at Ta
T_dot =@(t,T)(delta*Pel(T)-(Pc(T)+Pe(T)+delta*Pw(T)))/(m*ct); %insert function to be solved
% the function is the expression after (T,t)
% table title
%fprintf(fid,'%7s %7s %7s %7s %7s %7s %7s \n','i','t(i)','k1','k2','k3',
%'k4','y(i)');
for ii=1:1:numel(t)-1
k1 = T_dot(t(ii),T(ii));
k2 = T_dot(t(ii)+0.5*h,T(ii)+0.5*h*k1);
k3 = T_dot(t(ii)+0.5*h,T(ii)+0.5*h*k2);
k4 = T_dot(t(ii)+h,T(ii)+h*k3);
T(ii+1) = T(ii) + (h/6)*(k1+2*k2+2*k3+k4); % main equation
% table data
%fprintf(fid,'%7d %7.2f %7.3f %7.3f',ii, t(ii), k1, k2);
%fprintf(fid,' %7.3f %7.3f %7.3f \n', k3, k4, T(ii));
end
%T(numel(t))=[ ]; % erase the last computation of T(n+1)
% Solution PLOT:
plot(t,T,' ok')
title('RK-4--Numerical Solution---');
ylabel('T'); xlabel('t'); legend('numerical');
grid on
  13 Comments
Torsten
Torsten on 13 Jul 2022
Edited: Torsten on 13 Jul 2022
The heat transfer coefficient h had to be changed in h_value because h is the stepsize of the time integration.
Further, the t-array is already defined in the calling program - so I commented it out in the function.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!