MATLAB code has stopped working

Click For Summary

Discussion Overview

The discussion revolves around a MATLAB code that has stopped converging after a hard drive crash and data recovery. Participants explore potential reasons for the failure of the code to produce the expected results when solving an ordinary differential equation (ODE) using Newton's method.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports that their MATLAB program, which previously converged to an error of 10^-8 in a few iterations, now fails to converge after recovering it from a crashed hard drive.
  • Another participant inquires whether the program is being run on the same computer, suggesting that the environment might affect the code's performance.
  • The original poster confirms they are using the same computer and speculates that the recovery process may have corrupted the code.
  • One suggestion is made to have someone else run the code or to share it for external review, indicating a collaborative approach to troubleshooting.
  • The original poster mentions attempting to run the code on a different computer, but it still fails to converge, raising further questions about the integrity of the code or its dependencies.
  • The provided code includes detailed comments and a function definition, which some participants may analyze for potential issues.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the cause of the convergence issue. Multiple competing views regarding the potential reasons for the failure remain, including the possibility of code corruption and environmental factors.

Contextual Notes

The discussion highlights uncertainties regarding the integrity of the recovered code, the impact of the computational environment, and the specific conditions under which the code was previously successful.

hunt_mat
Homework Helper
Messages
1,816
Reaction score
33
A few months ago my hard drive crashed and I managed to recover the data and I tried to run some of my old programs (one solving an ODE via Newton's method. Before the crash, the error was 10^-8 after around 3 iterations, now it won't converge. It looks fine, ithe than writing the code from scratch again, I have no idea what to do.

Suggestions?
 
Physics news on Phys.org
Are you running it on the same computer?
 
I am.

The program had to be recovered from the failed hard drive, I am thinking that could be the reason why. It could be corrupted.
 
hunt_mat said:
I am.

The program had to be recovered from the failed hard drive, I am thinking that could be the reason why. It could be corrupted.

Can you get somebody else to run it or post it here for somebody to run / look at?
 
I tried to run it on another computer, previously it ran and converged within 4 iteration to an error of 10^-8, now it won't converge for some reason.

The code:
clear;
L=201; %Defines the number of steps, must always be an integer
x=linspace(-6,6,L); %Defines x
B=0.2; %This is the Bond number.
p=0.25*B*exp(-x.^2); %The pressure distribution
E_b=0.2; %This is the electric Bond number.
f=zeros(1,L+1);
f(1,1:L)=0.01*exp(-x.^2); %have some small initial guess for the wave profile
choice=false; %This selects the option to fix the Froude number of the minimum of the wave.
Q=-0.6; %This is the Froude number (choice=true), or the minimum of the profile (choice=false)
f(L+1)=Q; %this is the Foude number, either an exact value or an initial guess.
%The way forward is to use Newton's method to reduce the equation down to a linear algebra problem and then use Octave
%to solve the linear algrbra problem.

%The first step is to compute the Jacobian matrix
%Do this by diferencing between to close points and dividing through by that difference
df=10^-10;
J=zeros(L+1,L+1); %This will be the Jacobian.
X=zeros(L+1,1);
error=0.5;
while (error>10^-10)
for i=1:L+1
for j=1:L+1
T=zeros(1,L+1); %Set all the entries to zero again
T(j)=df;
J(i,j)=(g(x,f+T,p,Q,i,choice,B,E_b)-g(x,f-T,p,Q,i,choice,B,E_b))/(2*df); %Calculates the (i,j)th element of the Jacobian
end
end
for m=1:L+1
X(m)=-g(x,f,p,Q,m,choice,B,E_b);
end
%Now solve the system of equations
sol=J\X;
error=norm(X)
f=f+sol';
end

%Now plot the solution!
plot(x,f(1:L));
xlabel('x');
ylabel('f(x-ct)');

Using:

function r=g(x,f,p,Q,i,flag,B,E_b)
N=length(x);
h=1;
a_1=1-f(N+1);
a_2=h^4/90;
a_3=1.5/h;
a_4=-0.5*(B-1/3)*h^2;
a_5=0.5*E_b*h;
dx=x(2)-x(1);
ff=f(1:N);
f_ghost=[0 0 ff 0 0]; %Add in the ghost cells for f
%Prepare for the Hilbert transform
x_half=zeros(1,N-1);
f_half=zeros(1,N-1);
for k=1:N-1
x_half(k)=0.5*(x(k)+x(k+1));
f_half(k)=0.5*(ff(k)+ff(k+1));
end
grad=gradient(f_half,dx);
if (i<N+1)
hilbert=trapz(x_half,grad./(x_half-x(i)));
end
%Now comes the formulae for the finite differencing part of the algorithm.
if (i<N+1)
b_1=a_1*f_ghost(i+2);
b_2=(a_2/dx^4)*(f_ghost(i+4)-4*f_ghost(i+3)+6*f_ghost(i+2)-4*f_ghost(i+1)+f_ghost(i)); %Fourth order derivative
b_3=a_3*f_ghost(i+2)^2; %nonlinear term
b_4=(a_4/dx^2)*(f_ghost(i+3)-2*f_ghost(i+2)+f_ghost(i+1)); %Second order derivative
b_5=a_5*hilbert; %Hilbert transform term
b_6=p(i); %Pressure term
r=b_1+b_2+b_3+b_4+b_5+b_6;
else
if (flag==true)
r=f(N+1)-Q;
else
r=f(1+0.5*(N-1))-Q;
end
end
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
7
Views
9K
  • · Replies 1 ·
Replies
1
Views
9K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K