How to break a code when conditions are met

So I have a code with 2 numerical methods - backward euler and Runge kutta. What I want to do is create a program that calculates both numerical methods for a simple ODE and then stops when the difference between the two methods is 10e-5. The RK method is set for 11 iterations. The backward euler should end at approx 7380 iterations. How can this be done ?

1 commentaire

DIP
DIP le 8 Fév 2017
how do I know when should I stop iterations for the backward euler ??

Connectez-vous pour commenter.

Réponses (1)

KSSV
KSSV le 8 Fév 2017
Modifié(e) : KSSV le 8 Fév 2017
doc break
for i = 1:100
if i==7 % a condition
disp('I am exiting')
break
end
end

3 commentaires

% Euler’s Implicit Method and RK4
clear;close;clc;
u=1.5;
S=-33;
L=12/100;
N=11;
delx=L/(N-1);
K(1)=0.414;
y(1)=0;
C(1)=0.414;
x(1)=0;
A=10000;
delx1=L/(A-1);
for iter=1:inf % Loop for Implicit
%Euler Implicit
y(iter+1)=y(iter)+delx1;
syms a;
eqn= a ==(K(iter)+delx1*(a*S/u));
K(iter+1) = double(solve(eqn,a));
for i=1:N %loop for RK4
%4th Order RK Method
x(i+1)=x(i)+delx;
k1 = delx*(S/u*C(i));
k2 = delx*(S/u*C(i)+S/u*k1/2);
k3 = delx*(S/u*C(i)+S/u*k2/2);
k4 = delx*(S/u*C(i)+S/u*k3);
C(i+1) = C(i) + (1/6)*(k1+2*k2+2*k3+k4);
end
if abs((C(i+1)-K(iter+1))) <= 0.00001
break
end
end
plot(y,K,'-.+','color','g')
title('Concentration of CO vs. Distance');
xlabel('Axial(x) Direction [m]');
ylabel('Concentration of CO[mol/m3]');
hold on
plot(x,C,'-o','color','r')
KSSV
KSSV le 8 Fév 2017
What is the question? Did it work?
DIP
DIP le 8 Fév 2017
unfortunately it did not. The ODE is dC/dx=C*S/U .

Connectez-vous pour commenter.

Question posée :

DIP
le 8 Fév 2017

Commenté :

DIP
le 8 Fév 2017

Community Treasure Hunt

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

Start Hunting!

Translated by