How to avoid the warnings on my code
3 次查看(过去 30 天)
显示 更早的评论
Hello. I have the following code and i get a few warnings. How do i fix this problem ? (I used this warning('off','all')
warning but i think it's cheating and i do not prefer it)
%--- Solving the diff. eqn. y'(t) = y(t) - c*y^2(t) with c = 0.5 using
%--- Euler's method, Runge - Kutta method, and predictor-corrector method, and comparing in graph. I have four
%--- initial values for N0
clc, clear all, close all
tmax = 100;
t = [0:tmax];
N0 = [0.1 0.5 1.0 2.0];
f = @(t,N) N - 0.5*N.^2;
h = 0.1;
L = length(t)-1;
N = zeros(1,L);
%--- Euler's method ---
for i = 1:length(N0)
for j = 1:L
N(1) = N0(i);
N(1,j+1) = N(j) + h*f(':',N(j));
end
ylim([0 2.5])
subplot(1,3,1), plot(t,N), str = {'Euler''s method','for the following initial conditions'}; text(40,0.5,str); hold on
title('N'' = N - cN^2, c = 0.5')
xlabel('t'), ylabel('N(t)')
legend('y_0 = 0.1','y_0 = 0.5','y_0 = 1.0','y_0 = 2.0','Location','Best')
end
%--- Runge - Kutta method ---
for i = 1:length(N0)
N(1) = N0(i);
for j = 1:tmax
K1 = f(t(j),N(j));
K2 = f(t(j) + h*0.5, N(j) + h*K1*0.5);
K3 = f(t(j) + h*0.5, N(j) + h*K2*0.5);
K4 = f(t(j) + h, N(j) + h*K3);
N(j+1) = N(j) + h*((K1 + K4)/6 + (K2 + K3)/3);
end
ylim([0 2.5])
subplot(1,3,2), plot(t,N), str = {'Runge - Kutta method','for the following initial conditions'}; text(40,0.5,str); hold on
title('N'' = N - cN^2, c = 0.5')
xlabel('t'), ylabel('N(t)')
legend('y_0 = 0.1','y_0 = 0.5','y_0 = 1.0','y_0 = 2.0','Location','Best')
end
1 个评论
John D'Errico
2024-12-30
编辑:John D'Errico
2024-12-30
Lol. You can call it cheating to turn the warnings off. But I'd just call it dangerous in a sense, in that warnings were put there for a reason, to tell you that something bad is probably happening, and you should fix the problem.
Do you really want to turn off the warning light on your car that tells you it is overheating? Or just close your eyes to those warning signs about the bridge being out? ;-) I think you are right. Better to fix the problem.
采纳的回答
Torsten
2024-12-30
编辑:Torsten
2024-12-30
%--- Solving the diff. eqn. y'(t) = y(t) - c*y^2(t) with c = 0.5 using
%--- Euler's method, Runge - Kutta method, and predictor-corrector method, and comparing in graph. I have four
%--- initial values for N0
clc, clear all, close all
tmax = 100;
t = [0:tmax];
N0 = [0.1 0.5 1.0 2.0];
f = @(t,N) N - 0.5*N.^2;
h = 0.1;
L = length(t)-1;
N = zeros(1,L);
%--- Euler's method ---
for i = 1:length(N0)
for j = 1:L
N(1) = N0(i);
N(1,j+1) = N(j) + h*f(':',N(j));
end
subplot(1,3,1), plot(t,N), hold on
end
str = {'Euler''s method','for the following initial conditions'}; text(40,0.5,str);
ylim([0 2.5])
title('N'' = N - cN^2, c = 0.5')
xlabel('t'), ylabel('N(t)')
legend({'y_0 = 0.1','y_0 = 0.5','y_0 = 1.0','y_0 = 2.0'},'Location','Best')
%--- Runge - Kutta method ---
for i = 1:length(N0)
N(1) = N0(i);
for j = 1:tmax
K1 = f(t(j),N(j));
K2 = f(t(j) + h*0.5, N(j) + h*K1*0.5);
K3 = f(t(j) + h*0.5, N(j) + h*K2*0.5);
K4 = f(t(j) + h, N(j) + h*K3);
N(j+1) = N(j) + h*((K1 + K4)/6 + (K2 + K3)/3);
end
subplot(1,3,2), plot(t,N), hold on
end
str = {'Runge - Kutta method','for the following initial conditions'}; text(40,0.5,str);
ylim([0 2.5])
legend({'y_0 = 0.1','y_0 = 0.5','y_0 = 1.0','y_0 = 2.0'},'Location','Best')
title('N'' = N - cN^2, c = 0.5')
xlabel('t'), ylabel('N(t)')
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!