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!)?
EuroLion
EuroLion 2020 年 10 月 2 日
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.
EuroLion
EuroLion 2020 年 10 月 2 日
>> % 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!
Alan Stevens
Alan Stevens 2020 年 10 月 2 日
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 Exchange2-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