Octave function error

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Octave function error

Adrg01
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Octave function error

siko1056
On 3/30/20 3:23 PM, Adrg01 wrote:

> Hi
> I'm a new user of octave and i'm getting an error while trying to use a
> function. I've checked the forum for similar error. Even though the error in
> my case is same, i'm pretty sure it is not a formatting error and I've
> written the code in OctaveGUI.
>
> octave:9> val = costFunc(x,y,theta)
> error: 'costFunc' undefined near line 1 column 7
>
> Here is my code
> <https://octave.1599824.n4.nabble.com/file/t373708/func.png>
>
> function j=costFunc(x,y,theta)
> m=length(theta)+1
> yPred=(theta')*x
> temp=(yPred-y).*(yPred-y)
> j=(1/2*m)*(sum(temp));
> endfunction
>

Did you save "costFunc" as m-file "costFunc.m" in the current working
directory?  You can assure this by typing

   >> pwd
   ans = /home/siko1056/work

   >> which costFunc
   'costFunc' is a function from the file /home/siko1056/work/costFunc.m

Then you have to define x, y, and theta and can call "costFunc" as you
have done before.

HTH,
Kai




Reply | Threaded
Open this post in threaded view
|

Re: Octave function error

Adrg01
No, but I have changed the search path in the directory using
% Octave search path(advanced)
addpath('<path>')



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Octave function error

nrjank
Administrator
On Mon, Mar 30, 2020 at 6:53 AM Adrg01 <[hidden email]> wrote:
No, but I have changed the search path in the directory using
% Octave search path(advanced)
addpath('<path>')


that error means that octave doesn't see the file locally, or in your path.  Can you tell us the location and filename of the the m-file you saved with the function?

what are the outputs of the following:

>> path

>> pwd

>> which costFunc


Reply | Threaded
Open this post in threaded view
|

Re: Octave function error

Adrg01
Hello
the directory was not added in the path. However on re-entering the addpath
function has fixed the error. Sorry for the trouble.



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html