Matlab Continuously Busy When Running Code
5 views (last 30 days)
Show older comments
Hello again,
I have finally got my code to somewhat run. Now when I run my code with a certain function call it says it is busy, but will never end. When I manually stop the program I get this:
Operation terminated by user during test (line 57)
In newton_part_a (line 52)
fguess2 = myfunc(deltax2);
I'm just wondering what is happening here?
This is the command I am putting in:
newton_part_a(@test, [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1], 0.00001, 0.0001)
Here is my file:
if true
%Define a function that will read in:
%an m file containing n number of nonlinear functions
%an initial guess for the functions
%A value that will determine if convergence to a solution has occured
%(tolerance)
%The value for iteration for each guess
function K = newton_part_a(myfunc, guess, epsilon, step)
n = length(guess); %define the number of functions based on the test file input
deltay = zeros(1,n); % delta F for initial Newton Raphson guess yguess = myfunc(guess); %Initial values for y matrix in forward substitution initialguess = zeros(1,n); Jacob = zeros(n,n); %Set up the initial Jacobian Matrix to a matrix of zeros func = 1; %set func to 1 so that while loop has a valid argument
while func %Find new value of delta y for i = 1:n %for number of equations in myfun, set delta y deltay(i) = initialguess(i) - yguess(i); end %Exit loop if delta y has converged to an acceptable tolerance (epsilon) if abs(deltay) < epsilon func = 0; break; end
%Develop the Jacobian matrix for i=1:n %Fill in the empty matrix by moving column by column, line by line for j =1:n x1 = guess(j); %set arguments for jacobian elements xplus = x1 + step; xminus = x1 - step;
deltax1 = guess;
deltax1(j) = xplus;
deltax2 = guess;
deltax2(j) = xminus;
fguess1 = myfunc(deltax1);
fguess2 = myfunc(deltax2);
guess1 = fguess1(i);
guess2 = fguess2(i);
%Find each element of the jacobian
J = (guess1 - guess2)/ (2*step);
Jacob(i,j) = J;
end
end
%Sparse Jacobian to make function faster
JJ = sparse(Jacob);
[L,U,P] = lu(JJ); %define lower and upper matrixes for LU factorization
b = deltay;
c = zeros(n,1);
b = deltay*P;
%forward substitution
for i=1:n
c(i)= (b(i)-L(i, :)*c)/L(i,i);
end
%set a matrix of zeros for new guess
newguess = zeros(n,1);
%backward substitution
for i=n:-1:1
newguess(i) = (c(i)-U(i,:)*newguess)/U(i,i);
guess(i) = guess(i) + newguess(i);
end
yguess = myfunc(guess);
end
%Output the value of the variables after convergence has occured
K = guess;
end
end
0 Comments
Answers (0)
See Also
Categories
Find more on Systems of Nonlinear 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!