Dont copy from Internet

profileMurthy
Assignmentreport.docx

Q.No.1

Implementation of Simpsons Function

1. First m-file for function (simp.m)

%%NAME%%

%MATLAB implementation of simpsons function

%This function computes the integral "I" via Simpson's rule in the interval

%[a,b] with n+1 equally spaced points.

%Where,

%f= can either be an anonymous function (e.g. f=@(x) sin(x)) or a vector containing equally spaced values of the function to be integrated

%a= Initial point of interval

%b= Last point of interval

%n= # of sub-intervals (panels), must be integer

function I=simp(integralFcn,a,b,n)

h=(b-a)/n;

Iodd = 0; %initializing

for i=1:n/2 %starting loop

Iodd=Iodd + integralFcn( a + (2*i - 1) * h );%to get continous values for odd multiples

end

Ieven = 0; %initializing

for i=1:(n-2)/2 %starting loop

Ieven=Ieven + integralFcn(a + (2*i) * h); %to get continous values for ever multiples

end

%to find I basic

I=h * ( integralFcn(a) + integralFcn(b) + (4*Iodd) + (2*Ieven) ) / 3;

end

2. Then another m file to call simpsons function (runnerSimpsons.m)

%%NAME%%

%-----------------------------------------------------------------

% To call the function we have earlier made in simp.m

clear all

close all

%here we use long format for large values

format long

%defining integralFunction

f=@(x) exp(x.^2);

%defining parameters

a=0;

b=2;

n=100;

%calling simpsons function

I=simp(f,a,b,n);

disp(I);%displaying the function values

OUTPUTS: value of simpson and also workspace is shown in below figure,

Q.No.2

Implementation of Euler’s Function

1. First m-file for function (euler.m)

%%NAME%%

%%%%%%%%%%%%EULER's%%%%%%%%%%%%%%%

%If x is a vector or matrix, euler returns Euler numbers or polynomials for each element of x . When you use the euler function to find Euler polynomials, at least one argument must be a scalar or both arguments must be vectors or matrices of the same size

%MATLAB implementation of euler function

function E=euler(dyOverdt,a,b,c,h) % saving the function arguments in derivative wrt t and a,b,b,h

n=(b-a)/h; % applying formula for n

Y=zeros(1,n); % getting the zeros for n

T=linspace(a , b , n); % it will creates linspace

Y(1)=c;

for j=1:n-1 % starting loop from 1 to n-1

Y(j+1)=Y(j)+h*dyOverdt(Y(j),T(j)); %basic function which calls value each time

end

E=[T' Y']; % determining eulers value

end

2. Then another m file to call Euler’s function (runnerEuler.m)

%%NAME%%

%h % step size

%x = % the range of x

%y = % allocate the result y

%y(1) = % the initial y value

%n =% the number of y value

%-----------------------------------------------------------------

% closing all and clearing the workspace

clear all

close all

% defining derivative and calling euler function on it

% global variables

dyOverdt = @(y,t) ( sin(t)-y ) * cos(t); % derivative w.r.t t

%global parameters

a=0;

b=2;

c=1;

h=0.1;

%determining YY

YY=euler(dyOverdt,a,b,c,h);

%first row of YY is ti(time) and second row is yi(variables)

ti = YY(:,1);

yi = YY(:,2);

%ploting function and data vectors

plot(ti,yi , '-Or') %plotting time vs y

hold on

fplot(@(x) sin(x)-1+2*exp(-sin(x)), [ 0,2] , 'b') % plotting the data

OUTPUT: Below shown diagram shows the implementation of Euler’s and YY values to plot a graph and also a fig is attached to prove the implementation.

2 | Page