Ejercicios Matlab

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Clase 1

Modelación de sistemas electromagnéticos


Módulo 4

Profesor:Héctor.

Nombre:Jessely Santiago Bahena

Ejercicio 1

clear; close all; clc;

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

plot(x2,y2,' -b',x4,y4, '-r ')


hold on
fplot(f2,[t0 tf], '--k ')
title('Ejercicio 1')
xlabel('Tiempo (s)')
ylabel('Velocidad (m/s)')
grid on
legend('R-K2 ', ' R-K4',' Sol Analitica','Location ', ' Best')

Warning: Ignoring extra legend entries.

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'])

Error using table


Wrong number of arguments.

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

clear; close all; clc;

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

plot(x2,y2,' -b',x4,y4, '-r ')


hold on
fplot(f2,[t0 tf], '--k ')
title('Ejercicio 2')
xlabel('Tiempo (s)')
ylabel('Velocidad (m/s)')
grid on
legend('R-K2 ', ' R-K4',' Sol Analitica','Location ', ' Best')
[x4' y4']

Ejercicio 3

clear; close all; clc;

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

plot(x2,y2,' -b',x4,y4, '-r ')


hold on
fplot(f2,[t0 tf], '--k ')
title('Ejercicio 3')
xlabel('Tiempo (s)')
ylabel('Velocidad (m/s)')
grid on
legend('R-K2 ', ' R-K4',' Sol Analitica','Location ', ' Best')
[x4' y4']

Ejercicio 4

clear; close all; clc;


f = @(x,y) y*cos(x);
f2 = @(x) exp(sin(x));
t0 = 0; tf = 4; delta = 0.1; y0 = 1;

% RK: a2=0; PM: a2=0.5; Ralston: a2=2/3; Heun: a2=1


[x2,y2] = RK2(f,t0,tf,delta,y0,1); % Heun
[x4,y4] = RK4(f,t0,tf,delta,y0); % RK4

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']

You might also like