Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GaussSeidel
- function GaussSeidel(A,b,E)
- [n,m]=size(A);
- if n~=m
- disp('Matricea sistemului nu este patratica')
- return
- end
- contor=0;
- % verificarea diagonal dominantei:
- for i=1:n
- suma=0;
- for j=1:n
- suma=suma+abs(A(i,j));
- end
- if(A(i,i)==0)
- disp('Metoda nu se aplica');
- return;
- end
- if suma<abs(A(i,i))
- contor=contor+1;
- end
- end
- if contor==n
- disp('Matricea e diagonal dominanta=> Se poate alpica metoda')
- else
- disp('Matricea NU este diagonal dominanta => NU se poate aplica metoda')
- return;
- end
- P=0;Q=0;
- %metoda iterativa
- X=zeros(n,1);
- error=ones(n,1);
- iteratie=0;
- while max(error)>E %conditia de terminare a procesului
- iteratie=iteratie+1
- Z=X; %salvarea valorii curente pentru a cacula eroarea mai tarziu
- for i=1:n
- P=0;Q=0; % cele 2 sume din formula
- X1=X; %salvam solutiile precedente
- for j=1:(i-1)
- P=P+A(i,j)*X(j);% prima suma din formula
- end
- for k=(i+1):n % a doua suma din formula
- Q=Q+A(i,k)*X1(k);
- end
- X(i)=(b(i)-P-Q)/A(i,i); %noile valori ale lui X
- end
- Xsol(:,iteratie)=X;
- error=sqrt((X-Z).^2) %calculul erorilor
- end
- disp('Solutia sistemului:')
- X=Xsol(:,iteratie)
- disp('precizia solutiilor calculate:')
- X_exact=inv(A)*b
- n=norm(X-X_exact);
- end
- /////////////////////////////////////////
- A=[4 3 -1; -1 1 1;1 0 3];
- if det(A)~=0
- b=[2;0;-1];
- X=inv(A)*b
- else
- disp('Sistemul nu este compatibil determinat.');
- end
- //////////////////////////////////////////
- //Runge Kutta
- Metoda Runge-Kutta de ordin 2 pentru ecuații diferențiale de ordin I
- function[x,y]=rk2(f1,y0,n,a,b)
- clc;
- I=[a b];
- h=(I(2)-I(1))/n;
- x=zeros(n,1);
- y=zeros(n,1);
- pas=1;
- x(pas)=I(1);
- y(pas)=y0(1);
- while x(pas)<=I(2)
- k1=feval('f1',x(pas),y(pas));
- k2=feval('f1',x(pas)+h,y(pas)+h*k1);
- y(pas+1)=y(pas)+((k1+k2)/2)*h;
- x(pas+1)=x(pas)+h;
- pas=pas+1;
- end;
- plot(x,y,'y-');
- Metoda Runge-Kutta de ordin 2 pentru sistem de ecuații diferențiale
- function [x,y,z]=rk2(f,g,y0,n,a,b)
- I=[a b];
- h=(I(2)-I(1))/n;
- x=I(1):h:I(2);
- y=[];
- z=[];
- x(1)=I(1);
- y(1)=y0(1);
- z(1)=y0(2);
- pas=2;
- while pas<=length(x)
- k1=feval('f',x(pas-1),y(pas-1),z(pas-1));
- l1=feval('g',x(pas-1),y(pas-1),z(pas-1));
- k2=feval('f',x(pas-1)+h,y(pas-1)+h*k1,z(pas-1)+h*l1);
- l2=feval('g',x(pas-1)+h,y(pas-1)+h*k1,z(pas-1)+h*l1);
- y(pas)=y(pas-1)+((k1+k2)/2)*h;
- z(pas)=z(pas-1)+((l1+l2)/2)*h;
- pas=pas+1;
- end;
- x
- y
- z
- plot(x,y,'b-',x,z,'r--');
- Metoda Runge-Kutta de ordin 3 pentru ecuații diferențiale de ordin I
- function [x,y]=rk3(f,y0,n,a,b)
- clc;
- I=[a b];
- h=(I(2)-I(1))/n;
- x = zeros(n,1);
- y = zeros(n,1);
- pas=1;
- x(I)=I(1);
- y(pas)=y0(1);
- while x(pas)<=I(2)
- k1=feval('f',x(pas),y(pas));
- k2=feval('f',x(pas)+0.5*h,y(pas)+0.5*h*k1);
- k3=feval('f',x(pas)+h,y(pas)+2*h*k2-h*k1);
- y(pas+1)=y(pas)+(h*(k1+4*k2+k3))/6;
- x(pas+1)=x(pas)+h;
- pas=pas+1;
- end;
- plot(x,y,'r-');
- Metoda Runge-Kutta de ordin 3 pentru sistem de ecuații diferenţiale
- function [x,y,z]=rk3s(g,h,y0,n,a,b)
- clc
- I=[a b]
- h=(I(2)-I(1))/n;
- x=I(1):h:I(2);
- y=[];
- z=[];
- x(1)=I(1);
- y(1)=y0(1);
- z(1)=y0(2);
- pas=2;
- while pas<=length(x)
- k1=feval('g',x(pas-1),y(pas-1),z(pas-1));
- l1=feval('h',x(pas-1),y(pas-1),z(pas-1));
- k2=feval('g',x(pas-1)+h,y(pas-1)+h*k1,z(pas-1)+h*l1);
- l2=feval('h',x(pas-1)+h,y(pas-1)+h*k1,z(pas-1)+h*l1);
- k3=feval('g',x(pas-1)+h,y(pas-1)+2*h*k2-(h*k1),z(pas-1)+2*h*l2-(h*l1));
- l3=feval('h',x(pas-1)+h,y(pas-1)+2*h*k2-(h*k1),z(pas-1)+2*h*l2-(h*l1));
- y(pas)=y(pas-1)+(h*(k1+4*k2+k3))/6;
- z(pas)=y(pas-1)+(h*(11+4*l2+l3))/6;
- pas=pas+1;
- end;
- x
- y
- z
- plot(x,y,'b-',x,z,'r--');
- Metoda Runge-Kutta de ordin 4 pentru ecuații diferențiale de ordin I
- function [x,y]=rk4(f,y0,n,a,b)
- clc;
- I=[a b]
- h=(I(2)-I(1))/n;
- x =I(1):h:I(2);
- y = [];
- x(1)=I(1);
- y(1)=y0(1);
- for i=1:(length(x)-1)
- k1=feval('f',x(i),y(i));
- k2=feval('f',x(i)+(0.5*h),y(i)+(0.5*h*k1));
- k3=feval('f',x(i)+(0.5*h),y(i)+(0.5*h*k2));
- k4=feval('f',x(i)+h,y(i)+h*k3);
- y(i+1)=y(i)+(h*(k1+2*k2+2*k3+k4))/6;
- x(i+1)=x(i)+h;
- end;
- plot(x,y,'r-')
- Metoda Runge-Kutta de ordin 4 pentru sistem de ecuații diferențiale
- function [x,y,z]=rk2(g,h,y0,n,a,b)
- clc
- I=[a b]
- %calculam pasul de integrare
- h=(I(2)-I(1))/n;
- x=I(1):h:I(2);
- y=[];
- z=[];
- x(1)=I(1);
- y(1)=cond(1);
- z(1)=cond(2);
- pas=2;
- while pas<=length(x)
- k1=feval('g',x(pas-1),y(pas-1),z(pas-1));
- l1=feval('h',x(pas-1),y(pas-1),z(pas-1));
- k2=feval('g',x(pas-1)+0.5*h,y(pas-1)+0.5*h*k1,z(pas-1)+0.5*h*l1);
- l2=feval('h',x(pas-1)+0.5*h,y(pas-1)+0.5*h*k1,z(pas-1)+0.5*h*l1);
- k3=feval('g',x(pas-1)+0.5*h,y(pas-1)+0.5*h*k2,z(pas-1)+0.5*h*l2);
- l3=feval('h',x(pas-1)+0.5*h,y(pas-1)+0.5*h*k2,z(pas-1)+0.5*h*l2);
- k4=feval('g',x(pas-1)+h,y(pas-1)+h*k3,z(pas-1)+h*l3);
- l4=feval('h',x(pas-1)+h,y(pas-1)+h*k3,z(pas-1)+h*l3);
- y(pas)=y(pas-1)+(h*(k1+2*k2+2*k3+k4))/6;
- z(pas)=y(pas-1)+(h*(l1+2*l2+2*l3+l4))/6;
- pas=pas+1;
- end;
- x
- y
- z
- plot(x,y,'b-',x,z,'r--');
- Rezolvarea ecuațiilor diferențiale folosind funcțiile „ode23” și „ode45”
- ode23
- % conditia initiala
- tic23=tic;
- y0=1;
- % domeniul (intervalul)
- I=[1,2];
- % rezolvarea ecuatiei diferentiale
- [xval,yval]=ode23('f1',I,y0)
- t23=toc(tic23)
- % reprezentarea grafica a solutiei
- plot(xval,yval)
- ode45
- % conditiile initiale
- tic45=tic;
- y0=[0.1; 1];
- % domeniul (intervalul)
- I=[0,1];
- % rezolvarea ecuatiei diferentiale
- [xval,yval]=ode45('f1',I,y0)
- t45=toc(tic45)
- % reprezentarea grafica a solutiei
- plot(xval,yval(:,1),'b',xval,yval(:,2),'r--')
- legend('y1','y2')
- MetodaRunge-Kutta de ordin 2 pentru ecuații diferențiale de ordin I
- function[x,y]=rk2(f1,y0,n,a,b)
- clc;
- I=[a b];
- %calculăm pasul de integrare
- h=(I(2)-I(1))/n;
- x=zeros(n,1);
- y=zeros(n,1);
- pas=1;
- %începutul invervalului
- x(pas)=I(1);
- %setăm condiţia iniţială
- y(pas)=y0(1);
- %folosim metoda Runge-Kutta de ordinul 2
- while x(pas)<=I(2)
- k1=feval('f1',x(pas),y(pas));
- k2=feval('f1',x(pas)+h,y(pas)+h*k1);
- y(pas+1)=y(pas)+((k1+k2)/2)*h;
- x(pas+1)=x(pas)+h;
- pas=pas+1;
- end;
- %reprezentarea grafică a soluţiei
- plot(x,y,'y-');
- Metod aRunge-Kutta de ordin 3 pentru ecuații diferențiale de ordin I
- function [x,y]=rk3(f,y0,n,a,b)
- clc;
- I=[a b];
- %calculăm pasul de integrare
- h=(I(2)-I(1))/n;
- %alocam spaţiu pentru îmbunătăţirea timpului de execuţie
- x = zeros(n,1);
- y = zeros(n,1);
- pas=1;
- %începutul intervalului
- x(I)=I(1);
- %setarea condiţiei iniţiale
- y(pas)=y0(1);
- %folosim Runge-Kutta de ordin 3
- while x(pas)<=I(2)
- k1=feval('f',x(pas),y(pas));
- k2=feval('f',x(pas)+0.5*h,y(pas)+0.5*h*k1);
- k3=feval('f',x(pas)+h,y(pas)+2*h*k2-h*k1);
- y(pas+1)=y(pas)+(h*(k1+4*k2+k3))/6;
- x(pas+1)=x(pas)+h;
- pas=pas+1;
- end;
- %reprezentarea grafică
- plot(x,y,'r-');
- Metoda Runge-Kutta de ordin 4 pentru ecuații diferențiale de ordin I
- function [x,y]=rk4(f,y0,n,a,b)
- clc;
- I=[a b]
- %calculăm pasul de integrare
- h=(I(2)-I(1))/n;
- %alocare spaţiu pentru îmbunătăţirea timpului de execuţie
- x =I(1):h:I(2);
- y = [];
- %începutul intervalului
- x(1)=I(1);
- %setarea condiţiei iniţiale
- y(1)=y0(1);
- %folosim Runge-Kutta de ordin 4
- for i=1:(length(x)-1)
- k1=feval('f',x(i),y(i));
- k2=feval('f',x(i)+(0.5*h),y(i)+(0.5*h*k1));
- k3=feval('f',x(i)+(0.5*h),y(i)+(0.5*h*k2));
- k4=feval('f',x(i)+h,y(i)+h*k3);
- y(i+1)=y(i)+(h*(k1+2*k2+2*k3+k4))/6;
- x(i+1)=x(i)+h;
- end;
- %reprezentare grafică
- plot(x,y,'r-')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement