Reputation: 103
Please, I tried many ways to plot the function f(x,y)=(x*y)/(x^2 + y^2)
in 3D in Scilab but I couldn't.
My current code:
clearglobal
funcprot(0)
function z=f(x,y)
f(x,y)=(x*y)/(x^2 + y^2)
x=-10:10
y=-10:10
disp([(x,y),z])
endfunction
plot3d(z)
Current error:
plot3d: Wrong size for input argument #1.
Any help would be highly appreciated.
Upvotes: 0
Views: 1367
Reputation: 1010
There are two big mistakes in your code.
The syntax for defining functions is:
function output = function_name(arguments)
//computations go here
output = ...(something)...
endfuncfion
Then you could call it like this: var_A = function_name(2)
, and var_A
would then store the output of function_name()
when the argument is 2.
Your function would be correctly defined like this:
function z = f(x,y)
z = (x*y)/(x^2 + y^2)
endfunction
You shouldn't redefine x
and y
inside f()
like you did in x=-10:10
and y=-10:10
inside f()
. You actually can do it, but I suppose this not what you want.
plot3d()
When you name the output of f()
as z
, it doesn't mean that you already have z
values. It also doesn't mean that a variable called z
already existis. They way your code is, you're calling plot3d()
using an unexisting variable as its argument.
The name of the output doesn't matter once you run the code. Remember example of var_A
I gave above: you need to write a code that runs the function and creates a variable named z
that stores the values you want.
Perhaps you thought that if you assign values for x
and y
inside the implementation of f()
, the z
values would be calculated automatically, but that's not how programming languages usually work.
Now, after correcting your f()
, you can make a real surface plot:
x
and y
. The should be vectors (column or line matrices).f()
for every possible pair (x,y). The result of this loop will be a m
-by-n
matrix, where m
is the length of x
and n
, the length of y
;plot3d(x,y,z)
. Notice you have do pass x
and y
too, not just z
.This full code should probably work:
function z=f(x,y)
z=(x*y)/(x^2 + y^2);
endfunction
x = -10:10;
y = -10:10;
for i = 1 : length(x)
for j = 1 : length(y)
z(i,j) = f(x(i), y(j));
end
end
plot3d(x,y,z);
And the graphic output is:
Upvotes: 1