이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)
