Plot two variable sum function
3 views (last 30 days)
Show older comments
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)
0 Comments
Accepted Answer
Alan Stevens
on 2 Oct 2020
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 Comments
Alan Stevens
on 3 Oct 2020
Edited: Alan Stevens
on 3 Oct 2020
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:

More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

