I need help plotting

1 view (last 30 days)
Mircea Mircea
Mircea Mircea on 25 Apr 2022
Commented: Mathieu NOE on 26 Apr 2022
I don't know where the problem is. I can't get it to plot like this:
This is the code
clear
close all
Z1=500;
Z2=50;
Z3=500;
u=1;
tau1=(2*Z2)/(Z1+Z2);
tau2=(2*Z1)/(Z1+Z2);
tau3=(2*Z3)/(Z3+Z2);
p1=(Z2-Z1)/(Z2+Z1);
p2=(Z1-Z2)/(Z1+Z2);
p3=(Z3-Z2)/(Z3+Z2);
T=2*10^-6;
t=0:50*T/200:50*T;
% a voltage
n=linspace(2,25,length(t));
ua1=tau1*u.*(t>=0);
Uan=(tau1.*tau2.*(p3.^n-1).*(p2.^n-2).*u).*(t>=(n-1)*2*T);
% b voltage
n=linspace(1,25,length(t))
uab=(tau1.*tau3.*(p3.^n-1).*(p2.^n-1).*u).*(t>=n*2*T-T)
syms n % declare n as symbolic variable
F1=symsum(Uan,n,2,25);
F2=symsum(uab,n,1,25)
figure(1)
plot(t,F1+ua1, 'DisplayName', 'F1')
hold on
plot(t,F2, 'DisplayName', 'F2')
hold off
It plots like this:
And it should plot like this
Does anyone know how to fix this?I just started studying matlab and can't find an answer for it. Many thanks.
  1 Comment
Jason Shrand
Jason Shrand on 26 Apr 2022
I think part of the issue is this line of code here:
Uan=(tau1.*tau2.*(p3.^n-1).*(p2.^n-2).*u).*(t>=(n-1)*2*T);
and
uab=(tau1.*tau3.*(p3.^n-1).*(p2.^n-1).*u).*(t>=n*2*T-T)
I think you need parentheses around your exponents, like below:
Uan=(tau1.*tau2.*(p3.^(n-1)).*(p2.^(n-2)).*u).*(t>=(n-1)*2*T);
and
uab=(tau1.*tau3.*(p3.^(n-1)).*(p2.^(n-1)).*u).*(t>=n*2*T-T)
It still doesn't look like your picture, but maybe it's a step closer

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 26 Apr 2022
hello
this is the correct code
Uan and Uab needs to be 2D arrays (n rows and columns as many as length(t))
also some missing parenthesis as also found by @Jason Shrand
then its fairly straigthforward to do a (one direction) sum (without any need to sym operations)
clear
close all
Z1=500;
Z2=50;
Z3=500;
u=1;
tau1=(2*Z2)/(Z1+Z2);
tau2=(2*Z1)/(Z1+Z2);
tau3=(2*Z3)/(Z3+Z2);
p1=(Z2-Z1)/(Z2+Z1);
p2=(Z1-Z2)/(Z1+Z2);
p3=(Z3-Z2)/(Z3+Z2);
T=2*10^-6;
t=0:50*T/200:50*T;
% a voltage
n=(2:25)';
ua1=tau1*u.*(t>=0);
Uan=(tau1*tau2*(p3.^(n-1)).*(p2.^(n-2))*u).*(t>=(n-1)*2*T);
% b voltage
n=(1:25)';
uab = (tau1*tau3*(p3.^(n-1)).*(p2.^(n-1))*u).*(t>=n*2*T-T);
F1=sum(Uan,1);
F2=sum(uab,1);
figure(1)
plot(t,F1+ua1, 'DisplayName', 'F1')
hold on
plot(t,F2, 'DisplayName', 'F2')
hold off
  2 Comments
Mathieu NOE
Mathieu NOE on 26 Apr 2022
as always, my pleasure !

Sign in to comment.

More Answers (1)

Alan Stevens
Alan Stevens on 26 Apr 2022
More like this perhaps:
Z1=500;
Z2=50;
Z3=500;
u=1;
tau1=(2*Z2)/(Z1+Z2);
tau2=(2*Z1)/(Z1+Z2);
tau3=(2*Z3)/(Z3+Z2);
p1=(Z2-Z1)/(Z2+Z1);
p2=(Z1-Z2)/(Z1+Z2);
p3=(Z3-Z2)/(Z3+Z2);
T=2*10^-6;
t=0:50*T/200:50*T;
% a voltage
n1=2:25;
ua1= @(t) tau1*u.*(t>=0);
% b voltage
n2=1:25;
uab= @(t)sum((tau1.*tau3.*p3.^(n2-1).*p2.^(n2-1).*u).*(t>=(n2*2*T-T)));
figure(1)
plot(t,ua1(t) + Uanfn(n1,t,tau1,tau2,p2,p3,u,T), 'DisplayName', 'F1')
hold on
plot(t,uabfn(n2,t,tau1,tau3,p2,p3,u,T), 'DisplayName', 'F2')
hold off
function Uan = Uanfn(n,t,tau1,tau2,p2,p3,u,T)
Uan = 0;
for i = 1:numel(n)
Uan = (tau1.*tau2.*p3.^(n(i)-1).*p2.^(n(i)-2).*u).*(t>=(n(i)-1)*2*T) + Uan;
end
end
function uab = uabfn(n,t,tau1,tau3,p2,p3,u,T)
uab = 0;
for i = 1:numel(n)
uab = (tau1.*tau3.*p3.^(n(i)-1).*p2.^(n(i)-1).*u).*(t>=(n(i)*2*T-T)) + uab;
end
end

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by