Plot two variable sum function
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi can you tell me please how can I plot with 2 variables in sum function like this?

I need graph plot(omega,theta) and plot(omega,amplitude)
채택된 답변
Alan Stevens
2020년 10월 2일
Looks like you are translating from SMath Studio to MATLAB. The following is one way to do what you want:
% Basic data
beta(1) = 0.000125; lambda(1) = 0.0124;
beta(2) = 0.001424; lambda(2) = 0.0305;
beta(3) = 0.001274; lambda(3) = 0.111;
beta(4) = 0.002568; lambda(4) = 0.301;
beta(5) = 0.000748; lambda(5) = 1.13;
beta(6) = 0.000273; lambda(6) = 3;
% Define range of w
w = 0:0.001:1;
% Call function
[Amplitude, Theta] = fn(w,beta,lambda);
% Plot results
subplot(2,1,1)
plot(w,Amplitude),grid
xlabel('Omega'), ylabel('Amplitude')
subplot(2,1,2)
plot(w,Theta),grid
xlabel('Omega'), ylabel('Theta')
function [Amp, Theta] = fn(w,beta,lambda)
n = numel(w);
Amp = zeros(n,1);
Theta = zeros(n,1);
for i = 1:n
Re = sum(beta./(lambda.^2 + w(i)^2));
Im = w(i)*10^-3 + w(i)*sum(beta.*lambda./(lambda.^2 + w(i)^2));
Amp(i) = sqrt(Re^2 + Im^2);
Theta(i) = atan(Im/Re);
end
end
댓글 수: 17
EuroLion
2020년 10월 2일
Thank you for your answer. but called this error "Unrecognized function or variable 'fn'."
Alan Stevens
2020년 10월 2일
That's strange, because it works for me, giving

Did you copy and paste or rewrite it yourself?
EuroLion
2020년 10월 2일
Copied
Alan Stevens
2020년 10월 2일
Then it should work for you as well! Have you checked line-by-line (including the correct number of 'end')?
Try changing the name of the function (in both places where it is used).
EuroLion
2020년 10월 2일
Tried. I have two PC. same feedbacks :(
Alan Stevens
2020년 10월 2일
What version of MATLAB are you using (not that that should matter much!)?
R2020a. I think we ve problem with call function
Alan Stevens
2020년 10월 2일
편집: Alan Stevens
2020년 10월 2일
The words Call function should be commented out! Copy and paste your version of the code here.
Alan Stevens
2020년 10월 2일
Better to post the actual code, not a picture. Use the > button in the code section of the Comment menu.
>> % Basic data
beta(1) = 0.000125; lambda(1) = 0.0124;
beta(2) = 0.001424; lambda(2) = 0.0305;
beta(3) = 0.001274; lambda(3) = 0.111;
beta(4) = 0.002568; lambda(4) = 0.301;
beta(5) = 0.000748; lambda(5) = 1.13;
beta(6) = 0.000273; lambda(6) = 3;
% Define range of w
w = 0:0.001:1;
% Call function
[Amplitude, Theta] = fn(w,beta,lambda);
% Plot results
subplot(2,1,1)
plot(w,Amplitude),grid
xlabel('Omega'), ylabel('Amplitude')
subplot(2,1,2)
plot(w,Theta),grid
xlabel('Omega'), ylabel('Theta')
function [Amp, Theta] = fn(w,beta,lambda)
n = numel(w);
Amp = zeros(n,1);
Theta = zeros(n,1);
for i = 1:n
Re = sum(beta./(lambda.^2 + w(i)^2));
Im = w(i)*10^-3 + w(i)*sum(beta.*lambda./(lambda.^2 + w(i)^2));
Amp(i) = sqrt(Re^2 + Im^2);
Theta(i) = atan(Im/Re);
end
end
Unrecognized function or variable 'fn'.
Alan Stevens
2020년 10월 2일
I see the problem. You are running it directly in the workspace. You need to create a script (New script - top left hand corner of the Home section of the workspace menu), save it and run that.
EuroLion
2020년 10월 2일
Thank you. running! now I have another problem. How can I scale log x and y axis?
For example

Alan Stevens
2020년 10월 2일
In the workspace type help loglog, or help semilogx, or help semilogy as appropriate.
EuroLion
2020년 10월 2일
Thank u so much!
Also lookup logspace. As follows:
% Basic data
beta(1) = 0.000125; lambda(1) = 0.0124;
beta(2) = 0.001424; lambda(2) = 0.0305;
beta(3) = 0.001274; lambda(3) = 0.111;
beta(4) = 0.002568; lambda(4) = 0.301;
beta(5) = 0.000748; lambda(5) = 1.13;
beta(6) = 0.000273; lambda(6) = 3;
% Define range of w
w = logspace(-3,3,100);
% Call function
[Amplitude, Theta] = fn(w,beta,lambda);
% Plot results
subplot(2,1,1)
loglog(w,Amplitude),grid
xlabel('Omega'), ylabel('Amplitude')
subplot(2,1,2)
semilogx(w,Theta),grid
xlabel('Omega'), ylabel('Theta')
function [Amp, Theta] = fn(w,beta,lambda)
n = numel(w);
Amp = zeros(n,1);
Theta = zeros(n,1);
for i = 1:n
Re = sum(beta./(lambda.^2 + w(i)^2));
Im = w(i)*10^-3 + w(i)*sum(beta.*lambda./(lambda.^2 + w(i)^2));
Amp(i) = sqrt(Re^2 + Im^2);
Theta(i) = atan(Im/Re);
end
end
This produces

EuroLion
2020년 10월 3일
Now I need combine my plot with different coefficients. We have a constant for Im value. I need define an interval for this constant from 10^-8 to 10^-3. Like this
Im = w(i)*a + w(i)*sum(beta.*lambda./(lambda.^2 + w(i)^2));
a = 10^-8:10:10^-3;
And plot 6 curves in same graph.
Alan Stevens
2020년 10월 3일
편집: Alan Stevens
2020년 10월 3일
As follows
% Basic data
beta(1) = 0.000125; lambda(1) = 0.0124;
beta(2) = 0.001424; lambda(2) = 0.0305;
beta(3) = 0.001274; lambda(3) = 0.111;
beta(4) = 0.002568; lambda(4) = 0.301;
beta(5) = 0.000748; lambda(5) = 1.13;
beta(6) = 0.000273; lambda(6) = 3;
% Define range of w
w = logspace(-3,3,100);
a = logspace(-8,-3,6);
na = numel(a); nw = numel(w);
Amptude = zeros(nw,na);
Thta = zeros(nw,na);
for j = 1:na
% Call function
[Amplitude, Theta] = fn(w,beta,lambda,a(j));
Amptude(:,j)=Amplitude;
Thta(:,j) = Theta;
end
% Plot results
subplot(2,1,1)
loglog(w,Amptude),grid
xlabel('Omega'), ylabel('Amplitude')
subplot(2,1,2)
semilogx(w,Thta),grid
xlabel('Omega'), ylabel('Theta')
function [Amp, Theta] = fn(w,beta,lambda,a)
n = numel(w);
Amp = zeros(n,1);
Theta = zeros(n,1);
for i = 1:n
Re = sum(beta./(lambda.^2 + w(i)^2));
Im = w(i)*a + w(i)*sum(beta.*lambda./(lambda.^2 + w(i)^2));
Amp(i) = sqrt(Re^2 + Im^2);
Theta(i) = atan(Im/Re);
end
end
Results in:

추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기
참고 항목
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)
