How to use gradient?

34 views (last 30 days)
geometry geometry
geometry geometry on 13 May 2017
Commented: Star Strider on 15 May 2017
what is the problem with the following code?
f=input('enter function: ','s')
[DX,DY] = -gradient(f);
f = str2func(['@(x,y)' vectorize(f)]);
[DX,DY] = -gradient(f);
hold on
quiver(X,Y,DX,DY)
hold off
I get the following errors:
Error using zeros CLASSNAME input must be a valid numeric or logical class name.
Error in gradient (line 56) g = zeros(size(f),class(f)); % case of singleton dimension
  2 Comments
Stephen23
Stephen23 on 13 May 2017
Edited: Stephen23 on 13 May 2017
@geometry geometry: I changed the title of your question because the error has nothing to do with quiver: the error is in gradient (as the error message clearly states).
geometry geometry
geometry geometry on 14 May 2017
I'm sorry because i'm so new to matlab.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 13 May 2017
Edited: Star Strider on 13 May 2017
You have to evaluate ‘f’ to calculate the gradient.
The Code
f = 'log(x^2+y^2)';
f = str2func(['@(x,y)' vectorize(f)]);
x = -10:10;
y = x';
[DX,DY] = gradient(-f(x,y));
quiver(x,y,DX,DY,0); % The ‘0’ Argument Turns Off Arrow Scaling
The Plot
EDIT Added plot.
  7 Comments
geometry geometry
geometry geometry on 15 May 2017
Dear the problem is the line 15 of the following code (I wrote this line for quiver)
I have calculated -grad(f) in line 4 in terms of x,y which properly works.
I again calculate -grad(f) in line 15 in order to use it in quiver. but the problem is that this line just works for two variable functions and fails for one variable functions like x,x^2,... .
Here is the code:
syms x y;
f=input('enter function: ','s');
f = symfun(eval(f), [x y]);
g=[-diff(f,x),-diff(f,y)]; % line 4 calculated -grad(f)
f = str2func(['@(x,y)' vectorize(f)]);
[X,Y]=meshgrid(-10:.5:10);
figure
meshc(X,Y,f(X,Y));
figure
contour(X,Y,f(X,Y));
hold on
x = -10:10;
y = x';
[DX,DY] = gradient(-f(x,y)); %line 15 which works only for two variable functions
quiver(x,y,DX,DY,0);
hold off;
If I change the code to:
syms x y;
f=input('enter function: ','s');
f = symfun(eval(f), [x y]);
g=[-diff(f,x),-diff(f,y)];
f = str2func(['@(x)' vectorize(f)]); %this line changed
[X,Y]=meshgrid(-10:.5:10);
figure
meshc(X,Y,f(X));
figure
contour(X,Y,f(X));
hold on
x = -10:10;
y = x';
[DX] = gradient(-f(x)); %this line changed
quiver(x,DX,0); %this line changed
hold off;
It properly works for one variable functions. my question is that is there a way to change the code in such a way that it work properly for both 1 and 2 variable functions.
Star Strider
Star Strider on 15 May 2017
Consider using the symvar function (in core MATLAB as well as the Symbolic Math Toolbox) to determine the variables in an expression. You can then use the size or length of that result with appropriate if or similar blocks to calculate the appropriate gradient of your function.
Example
f = 'log(x^2+y^2)';
vars1 = symvar(f)
f = str2func(['@(x,y)' vectorize(f)]);
syms f(x,y) x y
f(x,y) = x^4 + tan(x);
vars2 = symvar(f)
vars1 =
2×1 cell array
'x'
'y'
vars2 =
x
Note that symvar will not work on function handles, so you must put it before your str2func call for it to work.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 13 May 2017
Edited: Stephen23 on 13 May 2017
You need to start reading the MATLAB documentation, because guessing how to use MATLAB will not be an efficient use of your time. MATLAB has very accessible documentation: learn to use it:
When you actually open the gradient documentation and read the input description, it clearly describes the first argument as "F — Input array vector | matrix | multidimensional array"
So far you have tried two different inputs, neither of which are numeric arrays:
f=input('enter function: ','s')
[DX,DY] = -gradient(f); % <- here the input is a string
f = str2func(['@(x,y)' vectorize(f)]);
[DX,DY] = -gradient(f); % <- here the input is a function handle
The gradient documentation does not state that it accepts strings or function handles. It accepts numeric data only. In fact, the subtitle at the very top of the page states quite clearly "Numerical gradient", and it does not state symbolic or functional gradient.
Perhaps you were trying to do something like this (note that this is a numeric calculation):
>> [X,Y] = meshgrid(0:9);
>> Z = X.^2 + sqrt(Y); % function of X and Y
>> [DX,DY] = gradient(Z); % input numeric array to gradient
>> quiver(X,Y,DX,DY)
  2 Comments
geometry geometry
geometry geometry on 14 May 2017
thanks I understood the problem but I wanted to use a code for any function type given in "input" not just for special function.
Stephen23
Stephen23 on 14 May 2017
@geometry geometry: use str2func to generate a function handle from the input string. I am sure that you can find and read the str2func documentation yourself.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!