Index Sizing of Matrices

2 次查看(过去 30 天)
Hello again,
I have been working on my Newton Raphson code a little more and I am running into another hiccup.
I have been putting this in my command line:
NR_PartA('test', logical([1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0]), 0.00001, 0.0001)
But now I am getting an error of:
"Index exceeds matrix dimensions" for line 20
yguess = myfunc(guess)
I know the matrix sizes do not match up so I'm wondering if I should be declaring a pre-defined matrix size for yguess or if there is another route I can take? Is it impossible for yguess to take the same size as calling my file with the functions with initial inputs?
I guess what I'm really confused about is calling the function. So when I call my 'test' file as i stated above, it is taking in strings. So how would it be able to read the equations found in that file and using my initial values to solve them? I put a simplified file with only two equations that I know the answer to below my code. Possibly some explanation on exactly what is going on when I call the function with my test file and numbers?
Thank you again for the help.
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 = NR_PartA(myfunc, guess , epsilon, step)
%maxiteration = 100; %define the maximum number of iterations for the function
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: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:1:n
%Fill in the empty matrix by moving column by column, line by line
for j =1: 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
if true
function f=test(x)
f(1)=x(1)*x(1)-2.*x(2)-6;
f(2)=2.*x(1)+x(2)*x(2)-33;
end

采纳的回答

Guillaume
Guillaume 2015-2-26
Advice: the first comment lines after the function declaration should be a description of the function followed by a description of each input and output argument, one per line, e.g.:
function K = NR_PartA(myfunc, guess , epsilon, step)
%NR_PartA Calculate ??? according to the Newton Raphson method
%myfunc: A string representing ????
%guess: A logical array representing ????
%epsilon: A scalar value representing ????
%step: A scalar value representing ????
A benefit of that is that if you get a useful output for help NR_PartA, but more importantly you document what inputs you expect. In particular, your first input is called myfunc, yet you pass a string. Shouldn't that be a function handle?
The error you're getting occurs before matlab even get a chance to try assign anything to yguess, so it has nothing to do with its size.
What myfunc(guess) does when myfunc is a string and guess is a logical array is simple return the characters of the myfunc string for which the corresponding value in guess is true. Of course, it there are more values in guess than there are characters in myfunc matlab is not happy.

更多回答(1 个)

Kody Haugli
Kody Haugli 2015-2-26
编辑:Guillaume 2015-2-27
I guess what I am confused about then is when I call my .m file with my functions and then put in my initial values, how does it know where to put my initial values in?
Say if my test file was:
function f=test(x)
f(1)=x(1)*x(1)-2.*x(2)-6;
f(2)=2.*x(1)+x(2)*x(2)-33;
end
How would this work when I call this .m file with the function? How does it work with the initial values I also call with the function?
  1 个评论
Guillaume
Guillaume 2015-2-27
Please use 'Comment on this Answer' to comment rather than starting a new answer.
I'm afraid I don't understand what you're asking here, probably because you're using the wrong terminology somewhere. In your example, what are you calling a function? Is it:
  • test(x)
  • f1(x) = x(1)^2 - 2*x(2) - 6
  • f2(x) = 2*x(1) + x(2)^2 - 33
  • something else?
I've got the feeling that you're trying to pass a function to another function, so that second function can evaluate the first with different values. If that's the case, then you need to learn about function handles.
Can you clarify?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by