Professional Documents
Culture Documents
Ejercicios Matlab
Ejercicios Matlab
Ejercicios Matlab
Profesor:Héctor.
Ejercicio 1
f = @(x,y) -y +sin(x);
f2=@(x) 1.5*exp(-x)+0.5*sin(x)-0.5*cos(x);
t0 = 4; tf = 9.6; delta = 0.1;
y0 = -1;
var3 = 1; %Heun
[x2,y2] = RK2(f,t0,tf,delta,y0,var3); %llamado a la función
[x4,y4]=RK4(f,t0,tf,delta,y0);%RK4
y_apr=f2(x4)
y_apr = 1×57
-0.0241 -0.0969 -0.1682 -0.2373 -0.3037 -0.3667 -0.4257 -0.4801
1
[x4' y4']
ans = 57×2
4.0000 -1.0000
4.1000 -0.9799
4.2000 -0.9672
4.3000 -0.9603
4.4000 -0.9579
4.5000 -0.9586
4.6000 -0.9613
4.7000 -0.9647
4.8000 -0.9680
4.9000 -0.9701
Time=x4;
AproxRK2=y2;
AproxRK4=y4;
Exacta=y_apr;
DifRK2=abs(y2-y_apr);
DifRK4=abs(y4-y_apr);
Tab=table('Tiempo','Exacta',['AproxRK2 ','AproxRK4','DiffRK2','Diff RK4'])
Caused by:
You might have intended to create a one-row table with the character vector 'Tiempo' as one
2
of its variables. To store text data in a table, use a string array or a cell array of character
vectors
rather than character arrays. Alternatively, create a cell array with one row, and convert that
to a table using CELL2TABLE.
PRK2=mean(DifRK2)
PRK4=mean(DifRK4)
%if PRK2>PRK4
%disp
Ejercicio 2
f = @(x,y) y.*tan(x)+x;
f2=@(x) x.*tan(x)+2.*sec(x).*6+1;
t0 = 0; tf = 3; delta = 0.1;
y0 = 10;
var3 = 1; %Heun
[x2,y2] = RK2(f,t0,tf,delta,y0,var3); %llamado a la función
[x4,y4]=RK4(f,t0,tf,delta,y0);%RK4
Ejercicio 3
f = @(x,y) ((3.*x)/y)-x.*y;
f2=@(x) sqrt(3+exp(-x.^2))
t0 = 0; tf = 4; delta = 0.1;
y0 =2;
3
var3 = 1; %Heun
[x2,y2] = RK2(f,t0,tf,delta,y0,var3); %llamado a la función
[x4,y4]=RK4(f,t0,tf,delta,y0);%RK4
Ejercicio 4
plot(x2,y2,'-b',x4,y4,'-r') % Grafica RK 2 y 4
hold on
fplot(f2,[t0 tf],'--k') % Grafica sol. real
title('Ejercicio 4')
xlabel('Eje x'); ylabel('Eje y')
grid on
legend('R-K2','R-K4','sol. analitica','location','best')
[x4' y4']