Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Function End

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

Zahid bin azam

1813025

Page2

Prb 1.
(I)
func 1
function file
function y=f(x);
y=sin(-2x)-sin(2x);
end

Script file

figure; hold on;


a=0; % start of interval
b=3.5; % end of interval
epsilon=0.000001; % accuracy value
iter= 50; % maximum number of iterations
tau=double((sqrt(5)-1)/2); % golden proportion coefficient,
around 0.618
k=0; % number of iterations
x1=a+(1-tau)*(b-a); % computing x values
x2=a+tau*(b-a);
f_x1=f(x1); % computing values in x points
f_x2=f(x2);
plot(x1,f_x1,'rx') % plotting x
plot(x2,f_x2,'rx')
while ((abs(b-a)>epsilon) && (k<iter))
k=k+1;
if(f_x1<f_x2)
b=x2;
x2=x1;
x1=a+(1-tau)*(b-a);

f_x1=f(x1);
f_x2=f(x2);

plot(x1,f_x1,'rx');
else
a=x1;
x1=x2;
x2=a+tau*(b-a);

f_x1=f(x1);
f_x2=f(x2);

plot(x2,f_x2,'rx')
end

k=k+1;
end
% chooses minimum point
if(f_x1<f_x2)
sprintf('x_min=%f', x1)
sprintf('f(x_min)=%f ', f_x1)
plot(x1,f_x1,'ro')
else
sprintf('x_min=%f', x2)
sprintf('f(x_min)=%f ', f_x2)
plot(x2,f_x2,'ro')
end

function file
function y=f(x);
y=-1*(sin(-2x)-sin(2x));
end

Script file

figure; hold on;


a=0; % start of interval
b=3.5; % end of interval
epsilon=0.000001; % accuracy value
iter= 50; % maximum number of iterations
tau=double((sqrt(5)-1)/2); % golden proportion coefficient,
around 0.618
k=0; % number of iterations
x1=a+(1-tau)*(b-a); % computing x values
x2=a+tau*(b-a);
f_x1=f(x1); % computing values in x points
f_x2=f(x2);
plot(x1,f_x1,'rx') % plotting x
plot(x2,f_x2,'rx')
while ((abs(b-a)>epsilon) && (k<iter))
k=k+1;
if(f_x1<f_x2)
b=x2;
x2=x1;
x1=a+(1-tau)*(b-a);

f_x1=f(x1);
f_x2=f(x2);

plot(x1,f_x1,'rx');
else
a=x1;
x1=x2;
x2=a+tau*(b-a);

f_x1=f(x1);
f_x2=f(x2);
plot(x2,f_x2,'rx')
end

k=k+1;
end
% chooses maximum point
if(f_x1<f_x2)
sprintf('x_max=%f', x1)
sprintf('f(x_max)=%f ', f_x1)
plot(x1,f_x1,'ro')
else
sprintf('x_max=%f', x2)
sprintf('f(x_max)=%f ', f_x2)
plot(x2,f_x2,'ro')
end

(ii)
func 2
function file
function y=f(x);
y= sin(-2*x).^3+(cos(4*x).^3);
end

Script file

figure; hold on;


a=0.5; % start of interval
b=2; % end of interval
epsilon=0.000001; % accuracy value
iter= 50; % maximum number of iterations
tau=double((sqrt(5)-1)/2); % golden proportion coefficient,
around 0.618
k=0; % number of iterations
x1=a+(1-tau)*(b-a); % computing x values
x2=a+tau*(b-a);
f_x1=f(x1); % computing values in x points
f_x2=f(x2);
plot(x1,f_x1,'rx') % plotting x
plot(x2,f_x2,'rx')
while ((abs(b-a)>epsilon) && (k<iter))
k=k+1;
if(f_x1<f_x2)
b=x2;
x2=x1;
x1=a+(1-tau)*(b-a);
f_x1=f(x1);
f_x2=f(x2);

plot(x1,f_x1,'rx');
else
a=x1;
x1=x2;
x2=a+tau*(b-a);

f_x1=f(x1);
f_x2=f(x2);

plot(x2,f_x2,'rx')
end

k=k+1;
end
% chooses minimum point
if(f_x1<f_x2)
sprintf('x_min=%f', x1)
sprintf('f(x_min)=%f ', f_x1)
plot(x1,f_x1,'ro')
else
sprintf('x_min=%f', x2)
sprintf('f(x_min)=%f ', f_x2)
plot(x2,f_x2,'ro')
end

function file
function y=f(x);
y= -1*(sin(-2*x).^3+(cos(4*x).^3));
end

Script file

figure; hold on;


a=0.5; % start of interval
b=2; % end of interval
epsilon=0.000001; % accuracy value
iter= 50; % maximum number of iterations
tau=double((sqrt(5)-1)/2); % golden proportion coefficient,
around 0.618
k=0; % number of iterations
x1=a+(1-tau)*(b-a); % computing x values
x2=a+tau*(b-a);
f_x1=f(x1); % computing values in x points
f_x2=f(x2);
plot(x1,f_x1,'rx') % plotting x
plot(x2,f_x2,'rx')
while ((abs(b-a)>epsilon) && (k<iter))
k=k+1;
if(f_x1<f_x2)
b=x2;
x2=x1;
x1=a+(1-tau)*(b-a);

f_x1=f(x1);
f_x2=f(x2);

plot(x1,f_x1,'rx');
else
a=x1;
x1=x2;
x2=a+tau*(b-a);

f_x1=f(x1);
f_x2=f(x2);

plot(x2,f_x2,'rx')
end

k=k+1;
end
% chooses maximum point
if(f_x1<f_x2)
sprintf('x_max=%f', x1)
sprintf('f(x_max)=%f ', f_x1)
plot(x1,f_x1,'ro')
else
sprintf('x_max=%f', x2)
sprintf('f(x_max)=%f ', f_x2)
plot(x2,f_x2,'ro')
end

%1st function
%minimum
f_1=@(x) sin(-2*x)+sin(2*x);
fplot(f_1,[0,3.5],'b')
grid on
[a,b]=fminbnd(f_1,0,3.5)

%maximum
f_1d=@(x) -(sin(-2*x))^3-(cos(4*x))^3;

[c,d]=fminbnd(f_1d,.0,3.5)

%2nd function
%minimum

f_2=@(x) (sin(-2*x))^3+(cos(4*x))^3;
fplot(f_2,[.5,2],'b')
grid on
[a,b]=fminbnd(f_2,.5,2)

%Maximum

f_2d=@(x) -(sin(-2*x))^3-(cos(4*x))^3;

[c,d]=fminbnd(f_2d,.5,2)

PRB 2

clc
clear all
f=@(x)[(x(1)^2)+x(1)*x(2)-10; 3*x(1)*x(2)^2+x(2)-57];
fsolve(f,[-5,5])
PRB 3
Function file
function dTdt=odeee(t,T)
%Given
h=890; c_B=896; rho_B=2707; r=0.2;

A_S=4*3.1416*r^2;
V_B=(4*3.1416/3)*r^3;
m_B=V_B*rho_B;

dTdtB=((h*A_S)*(T(2)-T(1)))/(m_B*c_B);
c_F=2050; R=0.5; L=0.5; rho_F=880;
VF=3.1416*R^2*L;
m_F=rho_F*VF;
dTdtF=((h*A_S)*(T(1)-T(2)))/(m_F*c_F);
dTdt=[dTdtB;dTdtF];
end

Script file
clc
clear all
t1=0:1:1000;
To=[150 20];
[t,T]=ode45(@odeee,t1,To);
plot(t,T,'linewidth',2)
grid on
xlabel('Time')
ylabel('Temperature')
legend('Ball','Fluid')
PRB 4
Function file
function dcdt=oda(t,c)
dcdt=[-0.013*c(1) - 1000*c(1)*c(3); -2500*c(2)*c(3); 0.013*c(1) -
1000*c(1)*c(3) - 2500*c(2)*c(3)];
end
script file

clc
t=0: 0.1:50;
c01=1;
c02=1;
c03=0;
c=[c01,c02,c03]
[tm,xsolve]=ode45(@oda,t,c)
r=xsolve(:,1)
y=xsolve(:,2)
plot(t,r,'-b',t,y,'linewidth',3)
grid on

You might also like