How to avoid the warnings on my code

조회 수: 2 (최근 30일)
Left Terry
Left Terry 2024년 12월 30일
편집: John D'Errico 2024년 12월 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
Warning: Ignoring extra legend entries.
Warning: Ignoring extra legend entries.
Warning: Ignoring extra legend entries.
%--- 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
Warning: Ignoring extra legend entries.
Warning: Ignoring extra legend entries.
Warning: Ignoring extra legend entries.
  댓글 수: 1
John D'Errico
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
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개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by