problems

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

problems

Robert Setif
I would like to know why the file:
 "
  # hyp.m
  function r=hyp(x,y)
  # hypoténuse d'un triangle de côtés x et y
  r=sqrt(x*x+y*y)
  endfunction
 "
always alert :
    " 
   >> hyp
   error: 'x' undefined near line 4 column 8
   error: called from
    hyp at line 4 column 2
   error: evaluating argument list element number 1
   error: called from
    hyp at line 4 column 2
 "
Fortunately, this is not a crashing error.
That problem happens with all my files.
I do not know to declare in function-arguments a variable, 
 for instance x as real, y as integer, m as scalar.
 
I also would like to change at the screen the shape of result 
 from a  "for i=1:30,disp(i^2),endfor"  in order to 
 request in a list-shape rather that column-shape, 
 (with "disp" or another command).
 Regards
 Robert SETIF  [hidden email]

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problems

Carlo de Falco-2

On 29 Sep 2015, at 19:20, Robert Setif <[hidden email]> wrote:

> I would like to know why the file:
>  "
>   # hyp.m
>   function r=hyp(x,y)
>   # hypoténuse d'un triangle de côtés x et y
>   r=sqrt(x*x+y*y)
>   endfunction
>  "
> always alert :
>     "
>    >> hyp
>    error: 'x' undefined near line 4 column 8
>    error: called from
>     hyp at line 4 column 2
>    error: evaluating argument list element number 1
>    error: called from
>     hyp at line 4 column 2
>  "
> Fortunately, this is not a crashing error.
> That problem happens with all my files.
> I do not know to declare in function-arguments a variable,
>  for instance x as real, y as integer, m as scalar.
>  
> I also would like to change at the screen the shape of result
>  from a  "for i=1:30,disp(i^2),endfor"  in order to
>  request in a list-shape rather that column-shape,
>  (with "disp" or another command).
>  Regards

Hi,

Your file defines a function which requires two input arguments,
if you call it without arguments the variables 'x' and 'y' will
not be defined in the internal scope, which results in this error.

Note that this is an error, not a warning, which means the function
is not run and return values are not set.

See this section of the manual for more information:
http://www.gnu.org/software/octave/doc/interpreter/Functions-and-Scripts.html#Functions-and-Scripts

HTH,
c.



_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problems

James Sherman
In reply to this post by Robert Setif
On Tue, Sep 29, 2015 at 1:20 PM, Robert Setif <[hidden email]> wrote:

> I would like to know why the file:
>  "
>   # hyp.m
>   function r=hyp(x,y)
>   # hypoténuse d'un triangle de côtés x et y
>   r=sqrt(x*x+y*y)
>   endfunction
>  "
> always alert :
>     "
>    >> hyp
>    error: 'x' undefined near line 4 column 8
>    error: called from
>     hyp at line 4 column 2
>    error: evaluating argument list element number 1
>    error: called from
>     hyp at line 4 column 2
>  "
> Fortunately, this is not a crashing error.
> That problem happens with all my files.
> I do not know to declare in function-arguments a variable,
>  for instance x as real, y as integer, m as scalar.
>
> I also would like to change at the screen the shape of result
>  from a  "for i=1:30,disp(i^2),endfor"  in order to
>  request in a list-shape rather that column-shape,
>  (with "disp" or another command).
>  Regards
>  Robert SETIF  [hidden email]
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave
>

Hi Robert,

You're getting 'x' undefined, because you're not passing in values to
your function.  When you define a function, say
------
function value = func(x,y,z)
value = x*y+z;
endfunction
------
what the first line indicates is the number of inputs and outputs that
your function expects.  In my case, it expects 3 inputs (or to be
specific, at most 3 inputs can passed to it.  There can be less). In
your case, it expects 2 inputs, x and y.

However, when you call your function hyp, you don't pass in any values
to it, so when the function is called, there are no values that Octave
can assign to x or y.  Thus, when you try to calculate the length of
the hypotenuse, Octave raises the error, 'x' is undefined. ('y' is
also undefined, but since x appears first, its the first error it
encounters).

This should be alleviated by passing x and y when you call the
function, such as:
> hyp(3,4)

Then this should return 5.

Hope this helps,

James Sherman

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problems

Doug Stewart-4
In reply to this post by Robert Setif


On Tue, Sep 29, 2015 at 1:20 PM, Robert Setif <[hidden email]> wrote:
I would like to know why the file:
 "
  # hyp.m
  function r=hyp(x,y)
  # hypoténuse d'un triangle de côtés x et y
  r=sqrt(x*x+y*y)
  endfunction
 "
always alert :
    " 
   >> hyp
   error: 'x' undefined near line 4 column 8
   error: called from
    hyp at line 4 column 2
   error: evaluating argument list element number 1
   error: called from
    hyp at line 4 column 2
 "
Fortunately, this is not a crashing error.
That problem happens with all my files.
I do not know to declare in function-arguments a variable, 
 for instance x as real, y as integer, m as scalar.
 
After you made the file hyp.m, you must use it this way:

hyp(3,4)
 
 
I also would like to change at the screen the shape of result 
 from a  "for i=1:30,disp(i^2),endfor"  in order to 
 request in a list-shape rather that column-shape, 

Try
i=1:30;
i.^2

 
 (with "disp" or another command).
 Regards
 Robert SETIF  [hidden email]

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave




--
DASCertificate for 206392


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problems

vicnorton
In reply to this post by Robert Setif

On Sep 29, 2015, at 1:20 PM, Robert Setif <[hidden email]> wrote:

> I would like to know why the file:
>  "
>   # hyp.m
>   function r=hyp(x,y)
>   # hypoténuse d'un triangle de côtés x et y
>   r=sqrt(x*x+y*y)
>   endfunction
>  "
> always alert :
>     "
>    >> hyp
>    error: 'x' undefined near line 4 column 8
>    error: called from
>     hyp at line 4 column 2
>    error: evaluating argument list element number 1
>    error: called from
>     hyp at line 4 column 2
>  "
> Fortunately, this is not a crashing error.
> That problem happens with all my files.
> I do not know to declare in function-arguments a variable,
>  for instance x as real, y as integer, m as scalar.
>  
> I also would like to change at the screen the shape of result
>  from a  "for i=1:30,disp(i^2),endfor"  in order to
>  request in a list-shape rather that column-shape,
>  (with "disp" or another command).
>  Regards
>  Robert SETIF  [hidden email]

I don't understand where your (first) problem is, Robert.

I created a file "hyp.m" in my Octave load path consisting of precisely

  # hyp.m
  function r=hyp(x,y)
  # hypoténuse d'un triangle de côtés x et y
  r=sqrt(x*x+y*y);
  endfunction

I see no problem with the following result.

octave> clear all
octave> side1 = 3;
octave> side2 = 4;
octave> hypotenuse = hyp(side1, side2)
hypotenuse =  5

- Vic


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problems

Juan Pablo Carbajal-2
In reply to this post by Robert Setif
On Tue, Sep 29, 2015 at 7:20 PM, Robert Setif <[hidden email]> wrote:

> I would like to know why the file:
>  "
>   # hyp.m
>   function r=hyp(x,y)
>   # hypoténuse d'un triangle de côtés x et y
>   r=sqrt(x*x+y*y)
>   endfunction
>  "
> always alert :
>     "
>    >> hyp
>    error: 'x' undefined near line 4 column 8
>    error: called from
>     hyp at line 4 column 2
>    error: evaluating argument list element number 1
>    error: called from
>     hyp at line 4 column 2
>  "
> Fortunately, this is not a crashing error.
> That problem happens with all my files.
> I do not know to declare in function-arguments a variable,
>  for instance x as real, y as integer, m as scalar.
>
> I also would like to change at the screen the shape of result
>  from a  "for i=1:30,disp(i^2),endfor"  in order to
>  request in a list-shape rather that column-shape,
>  (with "disp" or another command).
>  Regards
>  Robert SETIF  [hidden email]
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave
>
Could you send your file? or post it online?
If everything is alright you shouldn't get that error.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problems

Mike Miller-4
In reply to this post by Robert Setif
On Tue, Sep 29, 2015 at 19:20:02 +0200, Robert Setif wrote:

> I would like to know why the file:
>  "
>   # hyp.m
>   function r=hyp(x,y)
>   # hypoténuse d'un triangle de côtés x et y
>   r=sqrt(x*x+y*y)
>   endfunction
>  "
> always alert :
>     "
>    >> hyp
>    error: 'x' undefined near line 4 column 8
>    error: called from
>     hyp at line 4 column 2
>    error: evaluating argument list element number 1
>    error: called from
>     hyp at line 4 column 2
>  "
> Fortunately, this is not a crashing error.
> That problem happens with all my files.
> I do not know to declare in function-arguments a variable,
>  for instance x as real, y as integer, m as scalar.
The reason for the error is that you have not passed any arguments to
the function call. If you call hyp(3,4) then you should see a better
result.

All m-files in Octave first check for the correct number of arguments:

  if (nargin != 2)
    print_usage ();
  endif


> I also would like to change at the screen the shape of result
>  from a  "for i=1:30,disp(i^2),endfor"  in order to
>  request in a list-shape rather that column-shape,
>  (with "disp" or another command).

Do you perhaps want this?

  [1:30].^2

?

--
mike

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: problems

fbarbuto
In reply to this post by Robert Setif
Hi Robert,

You might write your function hyp() like this:

function r = hyp(x=0,y=0)
  r = sqrt(x*x + y*y);
end

That would prevent execution errors should you pass no arguments to it (which would be a bit of a
nonsense, though):

>> hyp
ans = 0
>> hyp()
ans = 0
>>

But it would work fine with some arguments:

>> hyp(3)
ans =  3

>> hyp(4)
ans =  4

>> hyp(3,4)
ans =  5

HTH,

Fausto