Polar : ERROR: Data must be numeric, datetime, duration or an array convertible to double.

2 views (last 30 days)
geometry geometry
geometry geometry on 24 May 2017
Answered: Walter Roberson on 24 May 2017
In this code I want to enter the function f(r) in terms of 'r' and then plot its figure with polarplot and calculate grad(f(r)) in terms of 'x,y' (I used chain theorem).
but when I run it and enter a function the following error occurs:
ERROR: Data must be numeric, datetime, duration or an array convertible to double.
Here is a code:
syms x y r;
f=input('enter function: ','s');
f = symfun(eval(f), r);
g=[-diff(f,r)*x*(x^2+y^2)^(-3/2),-diff(f,r)*y*(x^2+y^2)^(-3/2)];
f = str2func(['@(r)' vectorize(f)]);
[X,Y]=meshgrid(-10:.5:10);
figure
theta = 0:0.01:2*pi;
polarplot(theta,f(r));
what's the problem?

Answers (1)

Walter Roberson
Walter Roberson on 24 May 2017
You define f in terms of x and y, which are symbolic variables at the time you do the str2func(). You construct variables X and Y but you do not do anything with them. Yu construct theta, but you only use it as an arbitrary axes, disconnected from anything happening in f. You invoke f on the symbolic variable r.
Do not do the str2func: leave f as a symbolic formula, that you can then subs() values into. (But even then you have a disconnect between the mysterious theta, r, and X and Y.

Community Treasure Hunt

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

Start Hunting!