MATLAB code has stopped working

In summary: 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%Prep
  • #1
hunt_mat
Homework Helper
1,782
32
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
  • #2
Are you running it on the same computer?
 
  • #3
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.
 
  • #4
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?
 
  • #5
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
 

What could be causing my MATLAB code to stop working?

There can be several reasons why your MATLAB code has stopped working. It could be due to syntax errors, missing variables, or issues with the input data. It could also be caused by a bug in your code or compatibility issues with your MATLAB version.

How can I debug my MATLAB code that has stopped working?

You can use the MATLAB debugger tool to step through your code and identify any errors. You can also try using the "dbstop if error" command to automatically pause the code when an error occurs, allowing you to inspect the variables and identify the issue.

Can I recover my work if my MATLAB code has stopped working?

If your MATLAB code has stopped working and you have not saved your work, you can try using the "keyboard" command in the Command Window to pause the code and access the variables. You can then save your work and continue debugging the code.

How can I prevent my MATLAB code from stopping in the future?

To prevent your MATLAB code from stopping, it is important to test your code thoroughly before running it on large datasets. You can also use try-catch statements to handle errors and use good coding practices to avoid common mistakes. Additionally, keeping your MATLAB version up to date can help prevent compatibility issues.

Is there a way to automatically restart my MATLAB code if it stops working?

Yes, you can use the "try-catch" statement to automatically restart your code if it stops working. You can also use the "while" loop to continuously run your code until it successfully executes without any errors.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
988
  • Programming and Computer Science
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
9K
  • STEM Academic Advising
Replies
11
Views
1K
Replies
15
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
Back
Top