1
votes

I have a problem in Octave.

I would like to plot a function of two variables, but the function is given as function of a vector as in the example below function summe(x). I don't want to change the form of this function, since it appears in several other functions and I would have to rewrite it everywhere. I first tried the following code and some variants:

function sum =summe(x);
sum=x(1)+x(2);
endfunction
x1=0:1:1;
[X,Y]=meshgrid(x1,x1);
Z=summe([X,Y]);
contour(X,Y,Z) # does not work

I tried several things, also introducing here a function

function sum1=summe1(x,y)
sum1= summe([x,y])
endfunction

and to plot this function, but it does not work too. Is there no possibility to avoid making a loop? Thanks

1

1 Answers

1
votes

At the moment your summe function just adds the first two elements from whatever you pass to it. That's the only effect of the x(1)+ x(2) line. Since you haven't actually told us what mathematical function you are trying to produce with your code,it is hard to provide more guidance at the moment.

Also, if you do your function definition in a separate file instead of inline with your script, you won't have to rewrite it everywhere. The same one function will get read in all cases