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
Alan Stevens 2020년 10월 2일

1 개 추천

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
EuroLion 2020년 10월 2일
Thank you for your answer. but called this error "Unrecognized function or variable 'fn'."
Alan Stevens
Alan Stevens 2020년 10월 2일
That's strange, because it works for me, giving
Did you copy and paste or rewrite it yourself?
EuroLion
EuroLion 2020년 10월 2일
Copied
Alan Stevens
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
EuroLion 2020년 10월 2일
Tried. I have two PC. same feedbacks :(
Alan Stevens
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
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
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
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
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
Alan Stevens 2020년 10월 2일
In the workspace type help loglog, or help semilogx, or help semilogy as appropriate.
EuroLion
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
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
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에 대해 자세히 알아보기

태그

질문:

2020년 10월 2일

편집:

2020년 10월 3일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by