I need your help. y'(t) = -30*y+30*t^2+2*t; y(0)=1, & exact solution is y(t)=e^-30*x +x^2. i can't understand what is different b/w kr4 plotting & its error . i need both graph n error please send me code.

function rungekutta123
h = 0.05;
t = 0;
w = 1;
fprintf('Step 0: t = %12.8f, w = %12.8f\n', t, w);
for i=1:50
k1 = h*f(t,w);
k2 = h*f(t+h/2, w+k1/2);
k3 = h*f(t+h/2, w+k2/2);
k4 = h*f(t+h, w+k3);
w = w + (k1+2*k2+2*k3+k4)/6;
t = t + h;
fprintf('Step %d: t = %6.4f, w = %18.15f\n', i, t, w);
end
%%%%%%%%%%%%%%%%%%
function v = f(t,y)
v = -30*y+30*t^2+2*t;

 Accepted Answer

Are you trying to plot the result? If so, just save the integration step results in a matrix:
function rungekutta123
h = 0.05;
t = 0:0.05:2.5;
w(1) = 1;
fprintf('Step 0: t = %12.8f, w = %12.8f\n', t, w);
for ii=1:(length(t)-1)
k1 = h*f(t(ii),w(ii));
k2 = h*f(t(ii)+h/2, w(ii)+k1/2);
k3 = h*f(t(ii)+h/2, w(ii)+k2/2);
k4 = h*f(t(ii)+h, w(ii)+k3);
w(ii+1) = w(ii) + (k1+2*k2+2*k3+k4)/6;
fprintf('Step %d: t = %6.4f, w = %18.15f\n', ii, t(ii), w(ii));
end
y_exact = exp(-30.*t) + t.^2;
subplot(2,1,1)
plot(t,w,t,y_exact)
ylabel('Exact vs. approximated function')
subplot(2,1,2)
plot(t,w - y_exact)
ylabel('Error')
%%%%%%%%%%%%%%%%%%
function v = f(t,y)
v = -30*y+30*t^2+2*t;

4 Comments

Step 0: t = 0.00000000, w = 0.05000000 Step 0: t = 0.10000000, w = 0.15000000 Step 0: t = 0.20000000, w = 0.25000000 Step 0: t = 0.30000000, w = 0.35000000 Step 0: t = 0.40000000, w = 0.45000000 Step 0: t = 0.50000000, w = 0.55000000 Step 0: t = 0.60000000, w = 0.65000000 Step 0: t = 0.70000000, w = 0.75000000 Step 0: t = 0.80000000, w = 0.85000000 Step 0: t = 0.90000000, w = 0.95000000 Step 0: t = 1.00000000, w = 1.05000000 Step 0: t = 1.10000000, w = 1.15000000 Step 0: t = 1.20000000, w = 1.25000000 Step 0: t = 1.30000000, w = 1.35000000 Step 0: t = 1.40000000, w = 1.45000000 Step 0: t = 1.50000000, w = 1.55000000 Step 0: t = 1.60000000, w = 1.65000000 Step 0: t = 1.70000000, w = 1.75000000 Step 0: t = 1.80000000, w = 1.85000000 Step 0: t = 1.90000000, w = 1.95000000 Step 0: t = 2.00000000, w = 2.05000000 Step 0: t = 2.10000000, w = 2.15000000 Step 0: t = 2.20000000, w = 2.25000000 Step 0: t = 2.30000000, w = 2.35000000 Step 0: t = 2.40000000, w = 2.45000000 Step 0: t = 2.50000000, w = 1.00000000 ??? Undefined function or method 'f' for input arguments of type 'double'.
Error in ==> rungekutta1234 at 7 k1 = h*f(t(ii),w(ii)); thank you so much but, this is my answer and graph is not plot, error is also there please help me
sir i need a graph and [wrk4-wexact] error i means w_true, i hope you understand my problem.
I only showed the part of the code that needed changing. Should now be complete, see above.
Mischa kim, thank you so much for all of your help!

Sign in to comment.

More Answers (1)

y(t)=f(t, y(t)) y(t0)=y0 tE[t0, T] f(t,y)=-y/(2*t)+t^2 t0=1 T=2 y0=1

Categories

Find more on Programming in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!