use the MATLAB function trapez to evaluate the intergral that arisesin electrica
ID: 3110288 • Letter: U
Question
use the MATLAB function trapez to evaluate the intergral that arisesin electrical field theory
for the following values x and r
function trapez(f,a,b,n)
% Compute the integral of a f from a to b using the trapezoid rule
h=(b-a)/n;
disp('_________________________________________________________')
disp([' i xi f(xi) h=',num2str(h) ])
disp('_________________________________________________________')
S=feval(f,a);
fprintf(' %2.0f %12.4f %14.6f ',0,a,S);
for i=1:n-1
x=a+h*i;
g=feval(f,x);
S=S+2*g;
fprintf(' %2.0f %12.4f %14.6f ',i,x,g);
end
S=S+feval(f,b);
fprintf(' %2.0f %12.4f %14.6f ',n,b,feval(f,b));
INT=h*S/2;
fprintf(' The integral of f(x) is =%16.8f ',INT);
60r f2 11 _ (r)2sin do T.T 1-(-)-S272"O | d pi-T"JoExplanation / Answer
%%Without funcyion
x=input('Enter value x=');
r=input('Enter value r=');
f=@(phi) sqrt(1-((x/r)^2*(sin(phi))^2));
a=0;
b=2*pi;
n=100;
h=(b-a)/n;
phi=a:h:b;
for i=1:n+1
y(i)=f(phi(i));
end
disp('_________________________________________________________')
disp([' i xi f(xi) h=',num2str(h) ])
disp('_________________________________________________________')
for i=1:n+1
or
fprintf(' %2.0f %12.4f %14.6f ',i,phi(i),f(phi(i)));
end
s=0;
for i=2:n
s=s+y(i);
end
Int=(h/2)*(y(1)+s+y(n+1))
fprintf(' The integral of f(phi) is =%16.8f ',Int);
%or
% with function
clc;
clear all;
x=input('Enter value x=');
r=input(' Enter valuer=');
a=input('Enter value a=');
b=input('Enter value b=');
n=input('Enter value n=');
f=@(phi) sqrt(1-((x/r)^2*(sin(phi))^2));
trapiz(f,a,b,n)
% note 1 : this programm and below program you should save in one folder
%note 2 : save below program with name ' trapiz.m' otherwise it will not work above program. only you should run %above program
function Int=trapiz(f,a,b,n)
h=(b-a)/n;
phi=a:h:b;
for i=1:n+1
y(i)=f(phi(i));
end
disp('_________________________________________________________')
disp([' i xi f(xi) h=',num2str(h) ])
disp('_________________________________________________________')
for i=1:n+1
fprintf(' %2.0f %12.4f %14.6f ',i,phi(i),f(phi(i)));
end
s=0;
for i=2:n
s=s+y(i);
end
% result for n=10
The integral of f(phi) is = 3.05041254
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.