I am trying to reduce the step size for my inside for loop until the maximum difference between successive iterations is less than 0.1%. Is there a way for me to keep the values of y1 while reducing the step size for each run until the maxdifference?

1 view (last 30 days)
% Initialization
h = 0.5; %Step size
Ni = 50; %Number of steps
x(1:Ni) = 0;
y1(1:Ni) = 0;
% Initial conditions
x(1) = 1;
y1(1) = 0;
% Modified Euler Method
for j=2:Ni
for i=2:Ni
k1=(h)*(x(i-1)-2*x(i-1)*y1(i-1)-1)/(x(i-1)*x(i-1));
k2=(h)*((x(i-1)+(h))-2*(x(i-1)+(h))*(y1(i-1)+k1)-1)/((x(i-1)+(h))*(x(i-1)+(h)));
y1(i)=y1(i-1)+((k1+k2)/2); %Approximate y value
x(i)=x(i-1)+h; %x value
end
A(j)=diff(y1);
B(j)=max(A);
C(j)=(((B(j))-(B(j-1)))/(B(j-1)))*100;
if (C(j)>=0.1)
h=h/2;
else
end
end
plot(x(1:11))
plot(y1(1:11))

Answers (0)

Categories

Find more on Numerical Integration and Differential Equations 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!