Plot two variable sum function
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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!)?
EuroLion
2020 年 10 月 2 日
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.
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
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!
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
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!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)
