I need help plotting

조회 수: 5 (최근 30일)
Mircea Prisada
Mircea Prisada 2022년 4월 25일
댓글: Mathieu NOE 2022년 4월 26일
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
Jason Shrand
Jason Shrand 2022년 4월 26일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Mathieu NOE
Mathieu NOE 2022년 4월 26일
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
Mircea Prisada
Mircea Prisada 2022년 4월 26일
Your answer is easier to understand, thanks.
Mathieu NOE
Mathieu NOE 2022년 4월 26일
as always, my pleasure !

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alan Stevens
Alan Stevens 2022년 4월 26일
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

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by