You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Problem with plotting X @(theta)
1 view (last 30 days)
Show older comments
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)
Star Strider
on 19 Dec 2018
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
insaf
on 19 Dec 2018
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)
Star Strider
on 19 Dec 2018
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.
insaf
on 19 Dec 2018
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
Star Strider
on 19 Dec 2018
Edited: Star Strider
on 19 Dec 2018
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/199416/Problem%20with%20plotting%20X%20@(theta)%20-%202018%2012%2019.png)
insaf
on 20 Dec 2018
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
Star Strider
on 20 Dec 2018
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.
madhan ravi
on 20 Dec 2018
+1 this answer works for me without error
insaf
on 21 Dec 2018
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
Star Strider
on 21 Dec 2018
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.
madhan ravi
on 22 Dec 2018
Anytime :) , second Star Striders point.
madhan ravi
on 19 Dec 2018
Edited: madhan ravi
on 19 Dec 2018
str2double() % to convert string to double and then plot
16 Comments
madhan ravi
on 19 Dec 2018
please upload your code and datas
insaf
on 19 Dec 2018
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)
madhan ravi
on 19 Dec 2018
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)
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.
KALYAN ACHARJYA
on 19 Dec 2018
@insaf, Reagrding theta statement: both are same meaning, just representations are different.
Read Here
madhan ravi
on 19 Dec 2018
Edited: madhan ravi
on 19 Dec 2018
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
>>
madhan ravi
on 19 Dec 2018
Edited: madhan ravi
on 19 Dec 2018
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
Star Strider
on 19 Dec 2018
@insaf —
Please note that I figured out the reason your code was not working, and got it to work in my Answer.
Image Analyst
on 19 Dec 2018
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.?
Star Strider
on 19 Dec 2018
My solution apparently works in Octave.
madhan ravi
on 19 Dec 2018
@sir Image Analyst agree sometimes the OP is lucky but mostly not , functions are different as you mentioned.
insaf
on 20 Dec 2018
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
madhan ravi
on 20 Dec 2018
so see Star Striders answer
See Also
Categories
Find more on Octave in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)