% Initialisation: Function

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

function [tt,yout] = Rk4(tspan,pas,y0)

% initialisation

N = (tspan(2)-tspan(1))/pas;
N =fix(N);

narout=length(y0);
yout = zeros(N,narout);
tt = zeros(N,1);

y = y0;
N = 0;
%

for t =tspan(1):pas:tspan(2),
t

K1 = pas * eqdiff(y,t);
K2 = pas * eqdiff(y+0.5*K1,t+0.5*pas);
K3 = pas * eqdiff(y+0.5*K2,t+0.5*pas);
K4 = pas * eqdiff(y+K3,t+pas);

y0 = y + (K1 + 2*K2 + 2*K3 +K4)/6;

N = N+1;
tt(N,1) = t;

for j=1:narout
yout(N,j) = y(j);
end;
y=y0;

end

clear all;clc
% t initiale et t finale

%%%% données %%%%%%%%%


global R L Vm periode ws ;
fs=50;
R=1;
L=0.001;
Vm=220*sqrt(2);
periode =1/fs;
ws=2*pi*fs;
tfin=2*periode;
%%%%%%%%%%%%%%%%%%%%%%%
tspan=[0,tfin];

% pas de calcul
pas = periode/10000;

%-------------------------
%La solution par Runge-Kutta
N=1;
y0=zeros(N,1);

[tr,ii]=Rk4(tspan,pas,y0);

% %%%%%%%%%%% la tension V(t) %%%%%


ws=2*pi*fs;
vt1=Vm*sin(ws*tr);
vt2=Vm*sin(ws*tr-2*pi/3);
vt3=Vm*sin(ws*tr+2*pi/3);

%%%%%%%%%%% Vd(t) et Vcharge(t) %%%%


for indice=1:length(tr)
v1=Vm*sin(ws*tr(indice));
v2=Vm*sin(ws*tr(indice)-2*pi/3);
v3=Vm*sin(ws*tr(indice)+2*pi/3);
v01=Vm*sin(ws*tr(indice));
v02=Vm*sin(ws*tr(indice)-2*pi/3);
v03=Vm*sin(ws*tr(indice)+2*pi/3);
if v01>v02 & v01>v03
ud(indice)=v1;
vd1(indice)=0;

elseif v02>v03
ud(indice)=v2;
vd1(indice)=v1-v2;

else
ud(indice)=v3;
vd1(indice)=v1-v3;
end

end

%%%%%% ploting %%%%%


%%%% plot id(t) %%%
subplot(3,1,1);
h1=plot(tr,ud,tr,vt1,':',tr,vt2,':',tr,vt3,':');
set(h1(1),'linewidth',2);
grid on
%%%% plot vd(t)%%%
subplot(3,1,2);
h2=plot(tr,ii,tr,vt1,':',tr,vt2,':',tr,vt3,':');
set(h2(1),'linewidth',2);
grid on
%%%%% plot vcharge(t) %%%%
subplot(3,1,3);
h3=plot(tr,vd1,tr,vt1,':',tr,vt2,':',tr,vt3,':');
set(h3(1),'linewidth',2);
grid on

function dy=eqdiff(y,t)
global R L Vm ws;

v1=Vm*sin(ws*t);
v2=Vm*sin(ws*t-2*pi/3);
v3=Vm*sin(ws*t+2*pi/3);

if (v1>v2 && v1>v3)


ud=v1;
elseif v2>v3
ud=v2;
else
ud=v3;
end

dy = (ud- R*y)/L;

You might also like