現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Problem with plotting X @(theta)
2 ビュー (過去 30 日間)
古いコメントを表示
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
回答 (2 件)
Star Strider
2018 年 12 月 19 日
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 件のコメント
insaf
2018 年 12 月 19 日
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
2018 年 12 月 19 日
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
2018 年 12 月 19 日
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
2018 年 12 月 19 日
編集済み: Star Strider
2018 年 12 月 19 日
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
2018 年 12 月 20 日
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
2018 年 12 月 20 日
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.
insaf
2018 年 12 月 21 日
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
2018 年 12 月 21 日
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
2018 年 12 月 19 日
編集済み: madhan ravi
2018 年 12 月 19 日
str2double() % to convert string to double and then plot
16 件のコメント
insaf
2018 年 12 月 19 日
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"
insaf
2018 年 12 月 19 日
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
2018 年 12 月 19 日
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
2018 年 12 月 19 日
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
2018 年 12 月 19 日
編集済み: madhan ravi
2018 年 12 月 19 日
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
2018 年 12 月 19 日
@insaf, Reagrding theta statement: both are same meaning, just representations are different.
Read Here
madhan ravi
2018 年 12 月 19 日
編集済み: madhan ravi
2018 年 12 月 19 日
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
2018 年 12 月 19 日
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 ?
madhan ravi
2018 年 12 月 19 日
編集済み: madhan ravi
2018 年 12 月 19 日
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
2018 年 12 月 19 日
@insaf —
Please note that I figured out the reason your code was not working, and got it to work in my Answer.
Image Analyst
2018 年 12 月 19 日
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.?
madhan ravi
2018 年 12 月 19 日
@sir Image Analyst agree sometimes the OP is lucky but mostly not , functions are different as you mentioned.
insaf
2018 年 12 月 20 日
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
参考
カテゴリ
Help Center および File Exchange で Octave についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
