Problem with plotting X @(theta)

Hello there,
I have a function X(theta) defined by two others f(theta) and g(theta). the problem that I have is within plotting the X function. I use fplot command, but it shows me the error: "error: invalid conversion from string to real N-D array error". what does mean this? and how can I properly plot the function I want.
Thank you!
N.B: I use Octave

Answers (2)

Since you did not share your code, I can only guess what the problem is.
First, if you refer to a function within another function, you must call it as a function, just as you would in any other context.
Second, I am not familiar with Octave and its error messages.
Try this:
f = @(theta) sin(theta); % Create Function
g = @(theta) cos(theta); % Create Function
X = @(theta) f(theta) .* g(theta); % Create Function
figure
fplot(X)
grid

10 Comments

yes, the problem is exactly what you guessed:
hereafter is the code :
I tried the plot and fplot commands but in vain. I tried also using plot(vectorize(inline(x))) (as some friends told me) but no result
theta = linspace(-pi/2,pi/2,0.2);
r0=input ("Enter r0")
e=input ("Enter epsilon")
N = input ("Enter desired order")
k1 = [0:N]
k =find(mod(k1,2)==0)
f = @(theta) (4*r0/pi)*sum(arrayfun(sin(theta*k)/k,[0:N]))+e;
g = @(theta) (4*r0/pi)*sum(arrayfun(cos(theta*k),[0:N]));
x = @(theta) -f(theta)./(g(theta)+1)
I believe using arrayfun is inappropriate here. It seems that what you want is to simply do vector multiplication to produce a matrix, then sum that.
Try these:
r0=input ("Enter r0")
e=input ("Enter epsilon")
N = input ("Enter desired order")
k1 = [0:N];
k =find(mod(k1,2)==0)
f = @(theta) (4*r0/pi)*sum(sin(theta*k(:))./k(:))+e;
g = @(theta) (4*r0/pi)*sum(cos(theta.*k(:)));
x = @(theta) -f(theta)./(g(theta)+1)
figure
fplot(x, [0 10])
grid
It still throws a warning (in MATLAB). It nevertheless produces the plot, and that is the desired result.
Note that the ‘(:)’ subscript notation forces a column vector. If Octave does not have that option, use a simple transpose on ‘k’ instead.
This assumes that ‘theta’ as supplied to the function will always be a row vector.
I used a transpose on ‘k’ as you told me, but here is another error "error: operator *: nonconformant arguments (op1 is 5x1, op2 is 5x1)"
for sum(arrayfun(f)), I use it to sum all over the values of k, I don't know if using simply sum will do the same thing
Using sum in this context is likely appropriate.
The transpose may not be necessary, since fplot may have internal loops, and do element-wise operations by default.
This works for me (although MATLAB still throws warnings), and produces the plot:
f = @(theta) (4*r0/pi)*sum(sin(theta*k)/k)+e;
g = @(theta) (4*r0/pi)*sum(cos(theta*k));
The rest of the code in my previous Comment is unchanged.
EDIT —
The fplot function provides values for ‘theta’ here. The range is defined by the values in the vector [0 10].
Note that your linspace call produces an empty vector, at least in MATLAB.
The Plot —
Problem with plotting X @(theta) - 2018 12 19.png
in octave I don't get a plot, and the error still persists,
error: operator *: nonconformant arguments (op1 is 5x1, op2 is 6x1)
error: called from
starcomment>@<anonymous> at line 6 column 50
starcomment>@<anonymous> at line 9 column 23
fplot at line 136 column 8
starcomment at line 11 column 1
I will help you with this as much as I can.
Assuming that fplot considers ‘theta’ to be a row vector, transposing it to a column vector and keeping ‘k’ as a row vector to do the multiplication may work:
f = @(theta) (4*r0/pi)*sum(sin(theta'*k)/k)+e;
g = @(theta) (4*r0/pi)*sum(cos(theta'*k));
I looked at the Octave fplot help documentation. It gave no examples of something like this, and did not say how it worked internally.
+1 this answer works for me without error
Star Strider and madhan ravi thank you for your patience and help, I'll re-test the functions you give me and I look carefully at the size of the matrices while multiplying, maybe the error comes from this point
Thank you again
Our pleasure.
It would be nice to know how Octave’s fplot does its calculations. It appears not to to be as robust to row and column orientations as the MATLAB fplot function is.
Anytime :) , second Star Striders point.

Sign in to comment.

madhan ravi
madhan ravi on 19 Dec 2018
Edited: madhan ravi on 19 Dec 2018
str2double() % to convert string to double and then plot

16 Comments

Thank you for replying
I tried your suggestion, but this is not working, it shows me this "error: unary operator '.'' not implemented for 'function handle' operands"
please upload your code and datas
here is the code
theta = linspace(-pi/2,pi/2,0.2);
r0=input ("Enter r0")
e=input ("Enter epsilon")
N = input ("Enter desired order")
k1 = [0:N]
k =find(mod(k1,2)==0)
f = @(theta) (4*r0/pi)*sum(arrayfun(sin(theta*k)/k,[0:N]))+e;
g = @(theta) (4*r0/pi)*sum(arrayfun(cos(theta*k),[0:N]));
x = @(theta) -f(theta)./(g(theta)+1)
theta = -pi/2:.2:pi/2;
r0=3;
e=5 ;
N = 7;
k1 = 0:N;
k =find(mod(k1,2)==0);
f = (4*r0/pi)*sum(sin(theta.'*k)/k)+e;
g = (4*r0/pi)*sum(cos(theta.'*k));
x = -f./(g+1)
plot(x)
that's work! but for theta, is there any difference between the way I defined it (with linespace) and the way you write it ?
madhan ravi
madhan ravi on 19 Dec 2018
Edited: madhan ravi on 19 Dec 2018
0.2 is the step , in matlab 0.2 (should be an integer ) so I assumed you wanted the step 0.2 , the place where you put 0.2 in linspace represents number of intervals from the starting point to the end point.
@insaf, Reagrding theta statement: both are same meaning, just representations are different.
Read Here
Thanks Kalyan
See the illustration below in MATLAB (no idea about octave's linspace though):
>> linspace(-pi/2,pi/2,0.2)
ans =
1×0 empty double row vector
>> -pi/2:0.2:pi/2
ans =
Columns 1 through 7
-1.5708 -1.3708 -1.1708 -0.9708 -0.7708 -0.5708 -0.3708
Columns 8 through 14
-0.1708 0.0292 0.2292 0.4292 0.6292 0.8292 1.0292
Columns 15 through 16
1.2292 1.4292
>>
thank you @madhan ravi and @KALYAN ACHARJYA for your help! I appreciate that.
If I could ask, how can I add a loop "for" so it asks me each time to enter a new value for N, calculate and represent the new figure on the last one ?
The simplest way is to convert it as a function and just give the input N like shown below:
for i = 1:10
N=input('what value ? for N: ','s'); % if you just enter without anything the loop will stop
if isempty(N)==1
break
else
main(N) % function call
end
end
function main(N) % function definition
theta = -pi/2:.2:pi/2;
r0=3;
e=5 ;
k1 = 0:str2double(N);
k =find(mod(k1,2)==0);
f = (4*r0/pi)*sum(sin(theta.'*k)/k)+e;
g = (4*r0/pi)*sum(cos(theta.'*k));
x = -f./(g+1);
figure
plot(x)
end
@insaf —
Please note that I figured out the reason your code was not working, and got it to work in my Answer.
Could it be that your solutions are using MATLAB and he's not? He's using Octave. I'm not sure exactly how compatible it is, like do function names match up exactly, etc.?
My solution apparently works in Octave.
@sir Image Analyst agree sometimes the OP is lucky but mostly not , functions are different as you mentioned.
thank you Star Strider for your response, effectively, I noticed the error in defining theta. for the other code using "For", it doesn't work in octave since the "main" function is not defined there. I will search for an equivalent for it in octave, I think it will be easy then to implement a loop "for".
Mr madhan ravi what I'm wondering is the values I got using your code for f and g: as theta is defined in a large interval, f should then be a function of theta, but the code gives me a number! I guess because it calculates the summation along theta and k, while I want it to be just on k, so the f will be a function of theta.
the same thing with g, and x of course.
I hope my problem is clear for you
so see Star Striders answer

Sign in to comment.

Asked:

on 19 Dec 2018

Commented:

on 22 Dec 2018

Community Treasure Hunt

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

Start Hunting!