Reputation: 41
So I looked around to figure out what causes this error, and people keep on listing the culprit being matlab not knowing the directory path of the function. I checked and that doesn't seem to be the issue as matlab knows the path. I have some code here that is suppose to do Gauss elimination
classdef gaussfunctions
%UNTITLED Summary of this class goes here
% Detailed explanation goes here
properties
end
methods (Static)
function solution_vec = gauss_reduce(param_mat, const_vec)
%check for conistent sixe of param_mat and const_vec
[n,m] = size(param_mat);
if n ~= m | n ~= size(const_vec)
disp('Non square matrix or constant vector was incorrect size');
solution_vec = zeros(size(const_vec));
return
end
%reduce coeffeicient matrix to upper triangular
[ut_mat, new_const_vec] = ut_reduce(param_mat, const_vec);
%compute the solution vector
solution_vec = back_subst(ut_mat, new_const_vec);
end
function [ut_mat, new_const_vec] = ut_reduce(param_mat, const_vec)
%get the size of the parameter matrix
[m,n] = size(param_mat);
%get the values of the arguments to change and the return
cur_mat = param_mat;
cur_vec = const_vec;
%loop through all of the columns to reduce them
for cur_col = 1:m,
[cur_mat, cur_vec] = reduce_column(cur_mat, cur_vec, cur_col);
end
%return the values
ut_mat = cur_mat;
new_const_vec = cur_vec;
end
function [new_mat, new_const_vec] = reduce_column(param_mat, const_vec, column)
%get the size
[m,n] = size(param_mat);
%copy the argument values to change them and the return
cur_mat = param_mat;
cur_vec = const_vec;
%check to see if the pivot is zero or not
if param_mat(column,column) == 0
disp('Zero pivot encourtered');
new_mat = param_mat;
new_const_vec = zeros(size(const_vec));
return
end
%loops down calling reduce_row_at_col on all row below the
%current column
for i = column + 1:n,
[cur_mat, cur_vec] = reduce_row_at_col(cur_mat, cur_vec, column, column, i);
end
%return the end values
new_mat = cur_mat;
new_const_vec = cur_vec;
end
function [new_mat, new_const_vec] = reduce_row_at_col(param_mat, const_vec, col, row_added, row_reduced)
%transfer over the values
new_mat = param_mat;
new_const_vec = const_vec;
%get the multiple that will be multiplied by the row that will
%be added
m = (-param_mat(row_reduced,col))/param_mat(row_added, col);
%change the value of the new_mat at the row that's going to be reduced
new_mat(row_reduced,col) = param_mat(row_reduced,col) + m*param_mat(row_added,col);
%change the value of the new_const_vec at the row that's going
%to be reduced
new_const_vec(row_reduced) = const_vec(row_reduced) + m*const_vec(row_reduced);
end
function solution_vec = back_subst(ut_mat, new_const_vec)
%get the size of the matrix
[n,m] = size(ut_mat);
%set the partial soltuions to zeroes
part_solution_vec = zeros(new_const_vec);
%start from the last column and work backwards
for i = n: -1: 1,
part_solution_vec = back_subst_for_col(ut_mat, new_const_vec, i, part_solution_vec);
end
%return the final answer
solution_vec = part_solution_vec;
end
function new_part_solution_vec = back_subst_for_col(ut_mat, new_const_vec, column, part_solution_vec)
%get size
[n,m] = size(ut_mat);
%value used for calculation
subst_val;
%copy over the partial solution
new_part_solution_vec = part_solution_vec;
%if we are at the last element then we want to do a simplified
%calculation
if column == n
new_part_solution_vec(column) = new_const_vec(column)/ut_mat(column,column);
return
end
%otherwise calculate the soltuion through back substituion
for i = column + 1: n,
subst_val = subst_val + ut_mat(column,i)*part_solution_vec(i);
end
%do the final calculation
new_part_solution_vec(column) = (new_const_vec(column) - subst_val)/ut_mat(column,column);
end
end
end
So the problem is that a function can be called, but when it calls a helper function it will through an error. Here is an example of me calling gauss_reduce()
in the command window
gaussfunctions.gauss_reduce(A,B)
??? Undefined function or method 'ut_reduce' for input arguments of type 'double'.
Error in ==> gaussfunctions>gaussfunctions.gauss_reduce at 21
[ut_mat, new_const_vec] = ut_reduce(param_mat, const_vec);
Is it connected to me putting everything in one class? I've never programmed in matlab before so forgive me if it is a fairly obvious error, but I can't figure out what is going on.
Upvotes: 0
Views: 7260
Reputation: 46435
MATLAB differs from languages like C++ and Java® in that there is no special hidden class instance passed to all methods. You must pass an object of the class explicitly to the method. The left most argument does not need to be the class instance, and the argument list can have multiple objects.
from http://www.mathworks.com/help/matlab/matlab_oop/specifying-methods-and-functions.html
Also:
Local functions in classdef files are useful for utility functions that you use only within that file. These functions can take or return arguments that are instances of the class but, it is not necessary, as in the case of ordinary methods. For example, the following code defines myUtilityFcn outside the classdef block:
What this says is that if you put an end
before the utility function, it will be _outside the classdef and all will be well. It will be in the file, but outside of the class. Thus it is "hidden" from the world (which doesn't see beyond the class definition) but your class can use it:
% ... earlier code ...
%compute the solution vector
solution_vec = back_subst(ut_mat, new_const_vec);
end
end % <<<< add this one
function [ut_mat, new_const_vec] = ut_reduce(param_mat, const_vec)
%....
end
end % <<<<<< remove the last end statement
Upvotes: 2