Está en la página 1de 7

5

= 2
1
Matlab code for above integral (Lecture 19)
clear all
close all
clc
%interval
x_int = 1:5;
y = x_int.^2; % given function
%using trapz function
trapz_int = trapz(y);
fprintf('the value of functionis %3.4f\n',trapz_int)

if we have non-unit spacing instead of 1,5 we have


1,0.5,5
clear all
close all
clc
%interval
x_int = 1:0.1:5;
y = x_int.^2; % given function
%using trapz function
trapz_int = trapz(x_int,y);
fprintf('the value of functionis %3.4f\n',trapz_int)

Lecture 19 equation 45 solution (code )


5 3
= ( 2 + 2 )
5 3
clear all
close all
clc
%interval
x_int = linspace(-5,5);
y_int = linspace(-3,3);
[X,Y]= meshgrid(x_int,y_int);
% given function
F = X.^2+Y.^2;
%using trapz function
trapz_int = trapz(x_int,trapz(y_int,F,2));
fprintf('the value of functionis %3.4f\n',trapz_int)

Lecture 19 equation 46 solution (code ) using Quad


function
2
1
= 3
0 5 2
clear all
close all
clc
%interval
x_lower=0;
x_upper = 2;
%the function is
f = inline('1\(x.^3-5*x-2)');
%calling function
quad_int = quad(f,x_lower,x_upper);
fprintf('the value of functionis %3.4f\n',quad_int)

Lecture 21 equation 5 solution (code ) Eulers


Method
clear all
close all
clc
%Intervals and Step size
a =0;
b = 1;
x_0 = 1;
no_steps = 1;
step_size = (b-a)\no_steps;
%the function
f = @(x) x;
%apply euler formula
for n=1:no_steps;
x = x_0 +step_size*f(x_0);
x_0=x;
end
%dispaly result
disp(['The approx result is ',num2str(x)])

Lecture 21 slide NO 17 solution (code ) Eulers


Method find x(2) =? When X(1) = -4 and steps = 100
= 1 + 2 + 3
clear all
close all
clc
%Intervals and Step size
a =1;
b = 2;
no_steps = 100;
step_size = (b-a)\no_steps;
t_0 = a;
x_0 = -4;
%the function
f = @(x,t)1+x.^2+t.^3;
%apply euler formula
for n=1:no_steps;
x = x_0 +step_size*f(x_0,t_0);
x_0=x;
t_0 = t_0 + step_size;
end
%dispaly result
disp(['The approx result is ',num2str(x)])

Assignment 5 Codes
Q.4
clear all
close all
clc
%given limits
x=linspace(-1,1);
%function
f= 1\sqrt(1-x.^2);
%using trapeziod rule
result= trapz(x,f)

Q.5
clear all
close all
clc
%given limits
x=linspace(1,3);
y = linspace(0,3);
z=linspace(0,1);
%function Given
f = x.^2+y.^2+z.^2;
% By appliyingtrapezoidal rule
Result = trapz(x,f.*trapz(y,f).*trapz(z,f))

Q.6
clear all
close all
clc
%given limits
x=linspace(0,1);
y = linspace(0,1);
%function Given
f=(x.^2-10.*x.*y-y.^2).^4;
%by using trapeziod rule
Result=trapz(x,f.*trapz(y,f))

Assignment 6 Codes
Q.2
clear all
close all
clc
%given limits
a = 0;
b = -1;
n = 4;
h = (b-a)\n;
x_0 = 1;
%functition
f = @(t) 2*t-2+exp(-t);
for n= 1:n;
x = x_0+h*f(x_0);
x_o = x;
end
disp(['the value of function is',num2str(x)])

Q.4
clear all
close all
clc
%inital condtions
IC = [0,1\4];
x_lim = [0,20];
%calling function
[t,y] = ode45(@vdpol,x_lim,IC)
%Plot the solutions for $y_1$ and $y_2$ against t
plot(t,y(:,1),'-o',t,y(:,2),'-o')
title('Solution of van der Pol Equation (\mu = 1) with
ODE45');
xlabel('Time t');
ylabel('Solution y');
legend('y_1','y_2')
Function file Q.4
function dydt = vdpol(t,y)
dydt = [y(2); (1-y(1)^2)*y(2)-y(1)];

Q.5
clear all
close all
clc
x_i=0;
x_f=1;
x_s=[x_i x_f];

% initial conditions
Ic=[1; -2; -4];
%Calling Ode Solver
[x,y] = ode45(@func,x_s,Ic);

% Plotting
plot(x,y(:,1),'b -o',x,y(:,2),'r -o',x,y(:,3),'g -o')
title('Solution 3RD ORDER DE using ODE45');
xlabel('X');
ylabel('Solution Y');
Function file Q.5
function dydt = func(t,y)
dydt = zeros(3,1);
dydt(1) = y(2);
dydt(2) = y(3);
dydt(3) = 5*y(3) + 22*y(2) - 56*y(1);
end

También podría gustarte