Reputation: 123
I would like to create several matrices to represent Hilbert matrices of different orders. The matrix will then be called by a function. However, error messages appeared saying that the function(GE) is not defined. But if I put the function on top of the for loop, another similar message appeared saying that the input parameter(A) is not defined. May I know how I could solve this problem?
for k=2:12
H{k}=hilb(k);
x0(1:k)=1;%(1....1)
x0=x0(1:k);
b= H{k} .* x0'; %generate the n-vector b=Hx0
x_approx= GE(H{k},b);
endfor
function x_approx = GE(A,b)
Upvotes: 0
Views: 327
Reputation: 104555
You are trying to create a script file, but you are also trying to define a function within a script file which is not allowed. Script files are created to batch execute commands from the top of the file to the bottom. They are not designed to have functions defined in them and then you run the script.
Looks like I was wrong here. The above is for MATLAB, which I am fully aware of, but the behaviour is different for Octave. See carandraug's answer below. You need to define the function first followed by the code, but it also needs to have something at the beginning.
However, what I would recommend is shown below if you're a MATLAB developer.
Therefore, one way to fix this is to put GE
in a separate file called GE.m
and save it to the same working directory. Next, in that script file that you're working on, remove the definition of GE
, then try running your code again. It should work.
Specifically, in a file called GE.m
, place your GE
code in there:
%// GE.m definition
function x_approx = GE(A,b)
...
...
endfunction
Now, in your script file that you're working on, place the other code in there:
%// Script file stuff
for k=2:12
H{k}=hilb(k);
x0(1:k)=1;%(1....1)
x0=x0(1:k);
b= H{k} .* x0'; %generate the n-vector b=Hx0
x_approx= GE(H{k},b);
endfor
Now if you run the script file, it should work.
If you want to maintain everything in one file, wrap the entire file in a function
declaration and let it execute with no input arguments and the output arguments with whatever you want to be saved in the workspace, like so:
function x_val = test_GE
%// Script file stuff
for k=2:12
H{k}=hilb(k);
x0(1:k)=1;%(1....1)
x0=x0(1:k);
b= H{k} .* x0'; %generate the n-vector b=Hx0
x_val= GE(H{k},b);
endfor
endfunction
%// GE.m definition
function x_approx = GE(A,b)
...
...
endfunction
Note that I've changed the variable x_approx
in your script definition with x_val
to avoid conflicts with the output given by GE
. Also, what I wrote will only save the solution of the Hilbert matrix of 12 x 12
. If you want to save all of them, consider placing the results in a cell array like so:
function x_val = test_GE
%// Script file stuff
x_val = cell(11,1); %// Change
for k=2:12
H{k}=hilb(k);
x0(1:k)=1;%(1....1)
x0=x0(1:k);
b= H{k} .* x0'; %generate the n-vector b=Hx0
x_val{k-1}= GE(H{k},b); %// Change
endfor
endfunction
%// GE.m definition
function x_approx = GE(A,b)
...
...
endfunction
Now that you're done, run your script file and it should work. Remember, you can never mix the behaviour of script files and function files together. It has to be one or the other. See this post by gnovice for more details: In MATLAB, can I have a script and a function definition in the same file?
However, on the side, the only "function" you can define within a script file is an anonymous function, but that probably isn't your goal here.
Upvotes: 2
Reputation: 13091
You can define functions in Octave, that is not a problem. The issue is that the function needs to be defined before you use it.
#!/usr/bin/octave -qf
1;
function x_approx = GE(A,b)
## code for
endfunction
for k=2:12
H{k}=hilb(k);
x0(1:k)=1;%(1....1)
x0=x0(1:k);
b= H{k} .* x0'; %generate the n-vector b=Hx0
x_approx= GE(H{k},b);
endfor
Another thing is that in a script file, the first statement cannot be a function definition. The standard is to use 1;
if you want to start with a function. However, in most cases you will actually start with pkg load something;
or parsing of argv
Upvotes: 0