Problem with plotting X @(theta)
Afficher commentaires plus anciens
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
Réponses (2)
Star Strider
le 19 Déc 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 commentaires
insaf
le 19 Déc 2018
Star Strider
le 19 Déc 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
le 19 Déc 2018
Star Strider
le 19 Déc 2018
Modifié(e) : Star Strider
le 19 Déc 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 —
%20-%202018%2012%2019.png)
insaf
le 20 Déc 2018
Star Strider
le 20 Déc 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
le 20 Déc 2018
+1 this answer works for me without error
insaf
le 21 Déc 2018
Star Strider
le 21 Déc 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
le 22 Déc 2018
Anytime :) , second Star Striders point.
madhan ravi
le 19 Déc 2018
Modifié(e) : madhan ravi
le 19 Déc 2018
str2double() % to convert string to double and then plot
16 commentaires
insaf
le 19 Déc 2018
madhan ravi
le 19 Déc 2018
please upload your code and datas
insaf
le 19 Déc 2018
madhan ravi
le 19 Déc 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)
insaf
le 19 Déc 2018
madhan ravi
le 19 Déc 2018
Modifié(e) : madhan ravi
le 19 Déc 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
le 19 Déc 2018
@insaf, Reagrding theta statement: both are same meaning, just representations are different.
Read Here
madhan ravi
le 19 Déc 2018
Modifié(e) : madhan ravi
le 19 Déc 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
>>
insaf
le 19 Déc 2018
madhan ravi
le 19 Déc 2018
Modifié(e) : madhan ravi
le 19 Déc 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
le 19 Déc 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
le 19 Déc 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
le 19 Déc 2018
My solution apparently works in Octave.
madhan ravi
le 19 Déc 2018
@sir Image Analyst agree sometimes the OP is lucky but mostly not , functions are different as you mentioned.
insaf
le 20 Déc 2018
madhan ravi
le 20 Déc 2018
so see Star Striders answer
Catégories
En savoir plus sur Octave dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!