Hossein Gadri Elhattab
Hossein Gadri Elhattab
NUMERICAL ANALYSIS
HOME WORK
Id Number: 230100197
b)
a=0.5;
b=2.5;
c=4.5;
x1=(-b+sqrt(b^2-4*a*c))/(2*a)
x2=(-b-sqrt(b^2-4*a*c))/(2*a)
the output: x1=-1.4051 , x2=6.4051
c)
f=inline('-0.5*x^2+2.5*x+4.5');
xl=5;
xu=10;
c=f(xl);
d=f(xu);
if c*d >0
disp('no root')
end
for i=1:3
i
xr=(xl+xu)/2
et=abs((6.4051-xr)/6.4051)*1000
ea=abs((xu-xl)/(xu+xl))*100
y=f(xr);
if c*y<0
xu=xr; I Xl Xu Xr et ea
else 1 5 10 7.5 17.09 33.33
xl=xr; 2 5 7.5 6.25 2.4215 20
end 3 6.25 7.5 6.875 7.3363 9.09
end
a)
x=-10:0.3:10;
y=4.*x.^3-6.*x.^2+7.*x-2.3;
plot (x,y)
grid on
ROOT (4 , -6 , 7, -2.3)
b) f=inline('5*x^3-5*x^2+6*x-2');
xl=0;
xu=1;
c=f(xl);
d=f(xu);
e=10;
if c*d >0
disp('no root')
end
for i=1:6
xr=(xl+xu)/2
y=f(xr);
if (y=0.0) || (abs(xu-xl)/2) <=e
disp(c)
break
end I Xl Xu Xr e
else if c*y<0 1 0 1 0.5 -
xu=xr; 2 0 0.5 0.25 100%
else 3 0.25 0.5 0.375 33.33%
xl=xr;
4 0.375 0.5 0.4375 14.29%
end
end 5 0.375 0.4375 0.40625 7.69%
a)
x=-50:0.5:50;
y=0.7.*x.^5-8.*x.^4+44.*x.^3-90.*x.^2+82.*x-25;
plot(x,y)
grid on
a)
x=-15:0.5:15;
y=-2.75.*x.^3+18.*x.^2-21.*x-12;
plot(x,y)
grid on
x=-0.5:0.1:1;
y=sin(x)-x.^2;
plot(x,y)
grid on
-------------------------
syms x
y=input('enter f(x)=');
f=inline(y);
a=input('a=');
b=input('b=');
for i=1:100
E=less
i I a b X
then 2%
x=(b+a)/2; 1 0.5 1 0.75 -
y1=f(x);
2 0.75 1 0.875 14.29%
if abs(y1)<=2
disp('the root x=') 3 0.875 1 0.9375 6.67%
x 4 0.875 09375 0.90625 3.45%
break 5 0.875 0.90625 0.890625 1.75%
end
if f(a)*y1 <0
b=x;
continue
else
a=x;
continue
end
end
F(x)=Ln(x2)-0.7
Xl=0.5 , xu=2
a)
x=-0.2:0.1:10;
y=log(x.^2)-0.7;
plot(x,y)
grid on
b)
f=@(x) log(x^2)-0.7;
a=input('a= ')
b=input('b= ')
if(f(a)*f(b)<0)
for i=1:3
c=(a+b)/2;
e=abs((b-a)/(a+b))*100.0;
if(e<2)
disp©
break
end I a b c E
if(f(a)*f(c)<0)
b=c; 1 0.5 2 1.25 -
else 2 1.25 2 1.625 23.08%
a=c; 3 1.25 1.625 1.4375 13.04%
end
end
else
fprintf('No Root')
end
a)
x=-3:0.1:3;
y=(-0.3.*x+0.8)./x;
plot(x,y)
grid on
b)
f=@(x) (-0.3.*x+0.8)./x;
a=input('a= ')
b=input('b= ')
if(f(a)*f(b)<0)
for i=1:3
c=(a+b)/2;
e=abs((b-a)/(a+b))*100.0;
if(e<2)
disp(c)
break
end I a b c E=5%
if(f(a)*f(c)<0) 1 0 1 0.5 -
b=c; 2 0.5 1 0.75 33.33%
else
a=c; 3 0.75 1 0.875 14.29%
end 4 0.75 0.875 0.8125 7.69%
end 5 0.8125 0.875 0.8437 3.70%
else
fprintf('No Root ')
end
F(x)=-2x6-1.5x4+10x+2
Xl=0 , xu= 1
a)
x=-1:0.1:2;
y=-2.*x.^6-1.5.*x.^4+10.*x+2;
plot(x,y)
grid on
b)
f=@(x) -2*x^6-1.5*x^4+10*x+2
a=input('a=');
b=input('b=');
if(f(a)*f(b)<0) I a b c E=5%
for i=1:10 1 0 1 0.5 -
c=(a+b)/2; 2 0.5 1 0.75 33.33%
e=abs((b-a)/(a+b))*100;
3 0.75 1 0.875 14.29%
if(e<=5)
disp(c) 4 0.75 0.875 0.8125 7.69%
break 5 0.8125 0.875 0.8437 3.70%
end
if(f(a)*f(c)<0)
b=c;
else
a=c;
end
end
else
fprintf('No Root') end
x=0:0.4:10;
y=((9.81.*82)./c).*(1-exp((-c./82)*4))-36;
plot(c,y)
grid on
------------------
f=@(c) 9.81.*82)./c).*(1-exp((-c./82)*4))-36;
a=input('a=');
b=input('b=');
if(f(a)*f(b)<0)
for i=1:10
xr=(a+b)/2;
e=abs((b-a)/(a+b))*100;
if(e<2)
disp(xr)
break
end
if(f(a)*f(xr)<0)
b=xr; I a b Xr E=2%
else 1 3 5 4 -
a=xr; 2 3 4 3.5 14.29%
end
3 3.5 4 3.75 6.67%
end
else 4 3.5 3.75 3.625 3.45%
fprintf('No Root') 5 3.5 3.625 3.5625 1.75%
end
a)
h=0:0.4:15;
y=sqrt(2.*9.81.*h).*tanh((sqrt(2.*9.81.*h))./2.*l)-5;
plot(h,y)
grid on
b)
f=@(h) sqrt(2*9.81*h)*tanh((sqrt(2*9.81*h))/2*l)-5;
a=input('a=');
b=input('b=');
if(f(a)*f(b)<0)
for i=1:10
c=(a+b)/2;
e=abs((b-a)/(a+b))*100;
if(e<1)
disp(c) I a b c E=1%
break 1 0 2 1 100%
end 2 1 2 1.5 33.33%
if(f(a)*f(c)<0) 3 1 1.5 1.25 20%
b=c;
else 4 1.25 1.5 1.375 9.09%
a=c; 5 1.375 1.5 1.4375 4.35%
end 6 1.4375 1.5 1.4687 2.13%
end
7 1.4375 1.4687 1.4531 1.08%
else
fprintf('No Root') 8 1.4531 1.4687 1.4609 0.53%
end
a)
y=0:0.5:20;
f=1-(20.^2./(9.81.*(3.*y+(y.^2./2)).^3)).*(3+y);
plot(y,f)
grid on
b)
f=@(y) 1-(20.^2./(9.81.*(3.*y+(y.^2./2)).^3)).*(3+y);
a=input('a=');
b=input('b=');
if(f(a)*f(b)<0)
for i=1:10
c=(a+b)/2;
e=abs((b-a)/(a+b))*100;
if(e<1)
disp(c)
break
end
if(f(a)*f(c)<0)
b=c;
else
a=c;
end
end
else
fprintf('No Root')
end
I a b c E=1%
1 0.5 2.5 1.5 -
2 1.5 2.5 2 25%
3 1.5 2 1.75 14.29%
4 1.5 1.75 1.625 7.69%
5 1.5 1.625 1.5625 4%
6 1.5 1.5625 1.5312 2.04%
7 1.5 1.5312 1.5156 1.03%
8 1.5 1.5156 1.5078 0.52%
a)
x=0:0.5:22;
f=2.*x.^3-11.7.*x.^2+17.7.*x-5;
plot(x,f);
grid on
c)
format short
f=@(x) 2*x^3-11.7*x^2+17.7*x-5;
df=@(x) 6*x^2-23.4*x+17.7;
x0=input('x0=');
for i=1:50
i
if (abs(f(x0))<0.0001)
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
elseif ((abs(df(x0))<0.0001))
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
else
x1=x0-((f(x0))/(df(x0)))
f(x1)
if(abs(x1-x0)<0.0001)
fprintf('root= %f f(x)=%f',x0,f(x0))
break
end
x0=x1;
end
end
x0=input('x0=');
f=@(x) (0.9*x^2)+(1.7*x)+(2.5);
df=@(x) (1.8*x)+1.7;
for i=1:50
i
if (abs(f(x0))<0.0001)
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
elseif ((abs(df(x0))<0.0001))
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
else
x1=x0-((f(x0))/(df(x0)))
f(x1)
if(abs(x1-x0)<0.0001)
fprintf('root= %f f(x)=%f',x0,f(x0))
break
end
x0=x1;
end
end
a)
x=-5:0.5:5;
y=0.5.*x.^3-4.*x.^2+5.5.*x-1;
plot(x,y);
grid on
b)
x0=input('x0=');
f=@(x) (0.5*x^3)-(4*x^2)+(5.5*x)-1;
df=@(x) (1.5*x^2)-(8*x)+ 5.5;
for i=1:50
i
if (abs(f(x0))<0.01)
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
elseif ((abs(df(x0))<0.01))
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
else
x1=x0-((f(x0))/(df(x0)))
f(x1)
if(abs(x1-x0)<0.01)
fprintf('root= %f f(x)=%f',x0,f(x0))
break
end
x0=x1;
end
end
Result: i=4 , f ( -3.371198057489089e-06 ) = -0.00003 , root = 0.214332
f=@(x) (x^3)-(6*x^2)+(11*x)-6.1;
df=@(x) (3*x^2)- (12*x) +11;
for i=1:50
i
if (abs(f(x0))<0.01)
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
elseif ((abs(df(x0))<0.01))
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
else
x1=x0-((f(x0))/(df(x0)))
f(x1)
if(abs(x1-x0)<0.0001)
fprintf('root= %f f(x)=%f',x0,f(x0))
break
end
x0=x1;
end
end
a)
x=-6:0.3:6;
y=7.*sin(x).*exp(-x)-1;
plot(x,y);
grid on
b)
x0=input('x0=')
f=@(x) 7*sin(x)* exp(-x) -1
df=@(x) (-7*sin(x)*exp(-x)) + (7+cos(x)*exp(-x))
for i=1:50
i
if (abs(f(x0))<0.0001)
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
elseif ((abs(df(x0))<0.0001))
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
else
x1=x0-((f(x0))/(df(x0)))
f(x1)
if(abs(x1-x0)<0.0001)
fprintf('root= %f f(x)=%f',x0,f(x0))
break
end
x0=x1;
end
end
x0=input('x0=');
f=@(x) (exp(-0.5*x)*(4 - x) -2 );
df=@(x) (-2*exp(-0.5*x))+(0.5*x*exp(-0.5*x)) - (exp(-0.5*x));
for i=1:50
i
if (abs(f(x0))<0.0001)
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
elseif ((abs(df(x0))<0.0001))
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
else
x1=x0-((f(x0))/(df(x0)))
f(x1)
if(abs(x1-x0)<0.0001)
fprintf('root= %f f(x)=%f',x0,f(x0))
break
end
x0=x1;
end
end
x0=input('x0=')
f=@(x) 16.05*x^4 + 88.75*x^3 - 192.0375*x^2 + 116.35*x + 31.687
df=@(x) 64.05*x^3 +266.25*x^2 -384.075*x +116.35
for i=1:50
i
if (abs(f(x0))<0.0001)
fprintf('root= %f f(x)=%f\n',x0,f(x0))
fprintf('______________fx=0_________')
break
elseif ((abs(df(x0))<0.0001))
fprintf('root= %f f(x)=%f\n',x0,f(x0))
fprintf('______________df=0_________')
break
else
x1=x0-((f(x0))/(df(x0)))
f(x1)
if(abs(x1-x0)<0.0001)
fprintf('root= %f f(x)=%f',x0,f(x0))
fprintf('______________x1-x0=0_________')
break
end
x0=x1;
end
end
x0=input('x0=')
f=@(x) x^3 -2*x^2 -4*x + 8
df=@(x) 3*x^2 - 4*x - 4
for i=1:50
i
if (abs(f(x0))<0.0001)
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
elseif ((abs(df(x0))<0.0001))
fprintf('root= %f f(x)=%f\n',x0,f(x0))
break
else
x1=x0-((f(x0))/(df(x0)))
f(x1)
if(abs(x1-x0)<0.0001)
fprintf('root= %f f(x)=%f',x0,f(x0))
break
end
x0=x1;
end
end
format short
a=[8 2 -2;10 2 5;12 2 2];
b=[-2;4;6];
d=[a b]
row=size(d,1);
for i=1:row
[m,n]=max(abs(d(i:row,i)));
temp =d(i,:);
d(i,:)=d(n+i-1,:);
d(n+i-1,:)=temp
end
for i=1:row
for j=i+1:row
key=d(j,i)./d(i,i);
d(j,:)=d(j,:)- key.*d(i,:)
end
end
x=zeros(1,size(a,2));
for i=row:-1:1
hg=sum(d(i,i+1:end-1).*x(i+1:end));
x(i)=(d(i,end)-hg)./d(i,i);
end
fprintf('solution is x=%d \n',x)
format long
A=[10 2 -1;-3 -6 2;1 1 5];
B=[27;-61.5;-21.5];
x0=input(' first x0=');
y0=input(' seco x0=');
z0=input(' third x0=');
for i=1:100
i
x1=(27+2*y0-z0)/10;
x2=(61.5+3*x1+2*z0)/6;
x3=(-21.5-x1-x2)/5;
er1=abs(x1-x0);
er2=abs(x2-y0);
er3=abs(x3-z0);
if abs(x1-x0) <=5 & abs(x2-y0) <=5 & abs(x3-z0) <=5
disp('the solution x1-x2-x3 =')
x1
x2
x3
break
else
x0=x1;
y0=x2;
z0=x3;
end
end
format long
x0=input(' first x0=');
y0=input(' seco x0=');
z0=input(' third x0=');
for i=1:100
i
x1=(3+y0-z0)/6;
x2=(40-6*x1-z0)/9;
x3=(50-3*x1-x2)/12;
er1=abs(x1-x0);
er2=abs(x2-y0);
er3=abs(x3-z0);
if abs(x1-x0) <=5 & abs(x2-y0) <=5 & abs(x3-z0) <=5
disp('the solution x1-x2-x3 =')
x1
x2
x3
break
else
x0=x1;
y0=x2;
z0=x3;
end
end
format long
x0=input(' first x0=');
y0=input(' seco x0=');
z0=input(' third x0=');
for i=1:100
i
x1=(20+y0-2*z0)/8;
x2=(38+2*x1-z0)/6;
x3=(34-3*x1-x2)/-7;
er1=abs(x1-x0);
er2=abs(x2-y0);
er3=abs(x3-z0);
if abs(x1-x0) <=5 & abs(x2-y0) <=5 & abs(x3-z0) <=5
disp('the solution x1-x2-x3 =')
x1
x2
x3
break
else
x0=x1;
y0=x2;
z0=x3;
end
end
13.3) - Solve for the value of x that maximizes f(x) in Prob. 13.2 using the golden-section search. Employ initial
guesses of xl = 0 and xu=2 and perform three iterations.
function xopt=Golden_search f(xl,xu)
clc
clear all
f= @ (x) -1.5*x.^6 -2* x.^4 + 12.*x;
xm=0;
xu=2;
x= -1 : 0.1 :2 ;
for i = 1 : 100
d= 0.61805 * (xu-xm);
x1 = xm +d ;
x2 = xu - d;
if f(x1)>f(x2);
xm=x2;
xo=x2;
else
xu=x1;
xo=x1;
end
e0= 0.38195*(xu-xm)/xo;
if abs(e0)<0.001;
break
end
end
fprintf ( 'x=%f\n' , xo );
fprintf ( 'fx=%f\n' , f(xo));
fprintf ( 'i=%f\n' , i);
%plot(x , f(x))
%grid on
13.4 ) - Repeat Prob. 13.3, except use parabolic interpolation in the same fashion as Example 13.2. Employ
initial guesses of x0 = 0, x1 = 1, and x2 = 2 and perform three iterations.
clear
clc
x0=0;
x1=1;
x2=2;
f=@(x) 1.5*x^6 - 2*x^4 +12*x
dfx=@(x) 9*x^5 -8*x +12
for i=1:100
b=f(x0)*(x1^2-x2^2)+f(x1)*(x2^2-x0^2)+f(x2)*(x0^2-x1^2);
a=2*f(x0)*(x1-x2)+2*f(x1)*(x2-x0)+2*f(x2)*(x0-x1);
x3=b/a;
%fprintf('%f %f %f %f\n',x0,x1,x2,x3)
%pause
if(f(x3)>f(x1))
xopt=x3;
x0=x1;
x1=x3;
else
xopt=x1;
x2=x3 ;
end
fprintf('%f %f %f %f %f %f %f %f\n',x0,f(x0),x1,f(x1),x2,f(x2),xopt,f(xopt))
if(abs(dfx(xopt))<0.00001)
fprintf('%f %f %d\n',xopt,f(xopt),i)
break
end
end
fprintf('x_optimum=%f fx_optimum=%f\n',xopt,f(xopt))
f( 9.3899)=1012711.679881
13.5) - Repeat Prob. 13.3 but use Newton’s method. Employ an initial guess of x0 = 2 and perform three
iterations.
Clc
Clear all
x0=2 ;
fx=@(x) 1.5*x^6 - 2*x^4 +12*x ;
dfx=@(x) 9*x^5 -8*x +12 ;
d2fx=@(x)45*x^4 -8 ;
for i=1:100
if (abs(dfx(x0))<0.0001)
opt=x0;
break
end
x1=x0-(dfx(x0)/d2fx(x0));
x0=x1
end
i= 10
x0 = 2.2866
13.6 ) - Employ the following methods to find the maximum of f(x) = 4x -1.8x^2 + 1.2x^3 - 0.3x^4 (a) Golden-
section search (xl = -2, xu = 4, es = 1%).
(b) Parabolic interpolation (x0 = 1.75, x1 = 2, x2 = 2.5, iter = 4). Select new points sequentially as in the secant
method.
(c) Newton’s method (x0 = 3, es = 1%).
a)–
clc
clear all
xl=2;
xu=4;
f=@(x) - 0.3 *x^.4 +1.2*x^.3 -1.8*x^.2 +4*x ;
for i=1:100
d=0.6180*(xu-xl);
x1=xl+d;
x2=xu-d;
if(f(x1)>f(x2))
xopt=x1;
xl=x2;
else
xopt=x2;
xu=x1 ;
end
e=0.382*abs((xu-xl)/xopt) ;
if(e<=0.1)
fprintf('x_optimum=%f fx_optimum=%f\n',xopt,f(xopt))
break
end
pause
end
x_optimum=3.459021 fx_optimum=12.777441
f(3.459021) = 12.777441
b) –
clc
clear all
x0=1.75;
x1=2 ;
x2=2.5;
f=@(x) - 0.3 *x^.4 +1.2*x^.3 -1.8*x^.2 +4*x ;
dfx=@(x) -1.2*x^.3 +3.6*x^.2 - 3.6*x + 4 ;
for i=1:100
b=f(x0)*(x1^2-x2^2)+f(x1)*(x2^2-x0^2)+f(x2)*(x0^2-x1^2);
a=2*f(x0)*(x1-x2)+2*f(x1)*(x2-x0)+2*f(x2)*(x0-x1);
x3=b/a;
if(f(x3)>f(x1))
xopt=x3;
x0=x1;
x1=x3;
else
xopt=x1;
x2=x3 ;
end
fprintf('%f %f %f %f %f %f %f %f\n',x0,f(x0),x1,f(x1),x2,f(x2),xopt,f(xopt))
if(abs(dfx(xopt))<0.001)
fprintf('-----------------\n')
fprintf('%f %f %d\n',xopt,f(xopt),i)
fprintf('-----------------\n')
break
end
end
fprintf('x_optimum=%f fx_optimum=%f\n',xopt,f(xopt))
xopt = 2.780015118934841e+123 + 3.811968224317564e+123i
c) –
clc
clear all
x0=3 ;
fx=@(x) - 0.3 *x^.4 +1.2*x^.3 -1.8*x^.2 +4*x ;
dfx=@(x) -1.2*x^.3 +3.6*x^.2 - 3.6*x + 4 ;
d2fx=@ (x) -3.6*x^.2 + 7.2*x - 3.6 ;
for i=1:10
if (abs(dfx(x0))<0.01)
opt=x0;
break
end
x1=x0-(dfx(x0)/d2fx(x0));
x0=x1
pause
end
i=10
x0 = 6.7554
13.8) - Employ the following methods to find the maximum of the function from Prob. 13.7:
(a) Golden-section search (xl = -2, xu = 1, es = 1%).
(b) Parabolic interpolation (x0 = -2, x1 = -1, x2 = 1, iter = 4). Select new points sequentially as in the secant
method.
(c) Newton’s method (x0 = -1, es = 1%).
a) –
clc
clear all
xl=-2;
xu=1;
f=@(x) - x^.4 - 2*x^.3 - 8*x^.2 - 5*x ;
for i=1:100
d=0.6180*(xu-xl);
x1=xl+d;
x2=xu-d;
%pause
f(x1);
f(x2);
if(f(x1)>f(x2))
xopt=x1;
xl=x2;
else
xopt=x2;
xu=x1 ;
end
e=0.382*abs((xu-xl)/xopt)
if(e<=0.1)
fprintf('x_optimum=%f fx_optimum=%f\n',xopt,f(xopt))
break
end
pause
end
x_optimum=-1.729511 fx_optimum=-0.344314
f( -1.7295)=-0.344314
b)-
clear
clc
x0=-2;
x1=-1;
x2=1;
f=@(x) - x^.4 -2*x^.3 - 8*x^.2 -5*x
dfx=@(x) -4*x^.3 - 6*x^.2 -16*x -5
for i=1:100
b=f(x0)*(x1^2-x2^2)+f(x1)*(x2^2-x0^2)+f(x2)*(x0^2-x1^2);
a=2*f(x0)*(x1-x2)+2*f(x1)*(x2-x0)+2*f(x2)*(x0-x1);
x3=b/a;
%fprintf('%f %f %f %f\n',x0,x1,x2,x3)
%pause
if(f(x3)>f(x1))
xopt=x3;
x0=x1;
x1=x3;
else
xopt=x1;
x2=x3 ;
end
fprintf('%f %f %f %f %f %f %f %f\n',x0,f(x0),x1,f(x1),x2,f(x2),xopt,f(xopt))
if(abs(dfx(xopt))<0.001)
fprintf('%f %f %d\n',xopt,f(xopt),i)
break
end
end
fprintf('x_optimum=%f fx_optimum=%f\n',xopt,f(xopt))
x_optimum=-2635793474328200945042718
fx_optimum=1317896737164100527496940
f(-2635793474328200945042718) = 1317896737164100527496940
c)-
clc
clear all
x0=2-1 ;
fx=@(x) 1.5*x^6 - 2*x^4 +12*x ;
dfx=@(x) 9*x^5 -8*x +12 ;
d2fx=@(x)45*x^4 -8 ;
for i=1:10
if (abs(dfx(x0))<0.01)
opt=x0;
break
end
x1=x0-(dfx(x0)/d2fx(x0));
x0=x1
pause
end
i=10
x0 =
39.0321
13.11 ) -Determine the minimum of the function from Prob. 13.10 with the following methods:
(a) Newton’s method (x0 5=-1, es = 1%).
Clc
Clear all
x0=-1
fx=@(x) 3 + 6*x + 5*x^.2 + 3*x^.3 + 4*x^.4
dfx=@(x) 6 +10*x +9*x^.2 +16*x^.3
d2fx=@(x) 10 + 18*x + 48*x^.2
for i=1:10
if (abs(dfx(x0))<0.01)
opt=x0;
break
end
x1=x0-(dfx(x0)/d2fx(x0));
x0=x1
pause
end
i=10
x0 =
-7.5241 - 0.1797i
13.16) - Pressure measurements are taken at certain points behind an airfoil over time. These data best fi t
the curve y = 6 cos x - 1.5 sin x
from x = 0 to 6 s. Use four iterations of the golden-search method to find the minimum pressure. Set xl = 2
and xu = 4
clc
clear all
xl=2;
xu=4;
f=@(x) 6 *cos(x) - 1.5 *sin (x) ; %f=y
for i=0:6 ;
d=0.6180*(xu-xl);
x1=xl+d;
x2=xu-d;
f(x1);
f(x2);
if(f(x1)>f(x2))
xopt=x1;
xl=x2;
else
xopt=x2;
xu=x1 ;
end
e=0.382*abs((xu-xl)/xopt)
if(e<=0.1)
fprintf('x_optimum=%f fx_optimum=%f\n',xopt,f(xopt))
break
end
pause
end
x_optimum=3.527848 fx_optimum=-4.992874
f(3.5278)=-4.992874
13.18 ) -The deflection of a uniform beam subject to a linearly increasing distributed load can be computed as
y = W0/120EIL (-x^5 + 2L^2 x^3 – L^4 x)
Given that L = 600 cm, E =50,000 kN/cm^2 , I = 30,000 cm^4 , and w0 = 2.5 kN/cm, determine the point of
maximum defl ection (a) graphically, (b)
using the golden-section search until the approximate error falls below es = 1% with initial guesses of xl = 0
and xu = L .
clc
clear all
xl=input('xl=')
xu=input('xu=')
f=@(x) (((-2.3148*10^-14))*x^5 + ((1.666*10^8)*x^3) - ((3*10^-3)*x)) ; %f=y
for i=1:100
d=0.6180*(xu-xl);
x1=xl+d;
x2=xu-d;
%pause
f(x1);
f(x2);
if(f(x1)>f(x2))
xopt=x1;
xl=x2;
else
xopt=x2;
xu=x1 ;
disp('fx2>fx1');
end
e=0.382*abs((xu-xl)/xopt)
if(e<=0.01)
fprintf('x_optimum=%f fx_optimum=%f\n',xopt,f(xopt))
break
end
pause
end
x_optimum=592.108966 fx_optimum=34584373149231524.000000
f(592.108966)= 34584373149231524.000000
13.21 ) -An object can be projected upward at a specifi ed velocity. If it is subject to linear drag, its altitude as
a function of time can be computed as z = z0 +( m/ c) (V0 +( mg /c) * (1 – exp(c/m)t ) –( mg/ c )t
where z = altitude (m) above the earth’s surface (defi ned as z = 0), z0 = the initial altitude (m), m = mass (kg),
c = a linear drag coeffi cient (kg/s), v0 5 initial velocity (m/s), and t = time (s). Note that for this formulation,
positive velocity is considered to be in the upward direction. Given the following parameter values: g = 9.81
m/s2 , z0 = 100 m, v0 = 55 m/s, m 5 80 kg, and c = 15 kg/s, the equation can be used to calculate the jumper’s
altitude. Determine the time and altitude of the peak elevation (a) graphically, (b) analytically, and (c) with
the golden-section search until the approximate error falls below es = 1% with initial guesses of tl = 0 and tu
=10 s.
tl=input('tl=')
tu=input('tu=')
f=@(t) (100 + (572.0156)*(1 - exp(-0.1875*t)) -52.32*t) ; %f=y
for i=1:100
d=0.6180*(tu-tl);
t1=tl+d;
t2=tu-d;
f(t1);
f(t2);
if(f(t1)>f(t2))
topt=t1;
tl=t2;
else
topt=t2;
tu=t1 ;
end
e=0.382*abs((tu-tl)/topt)
if(e<=0.01)
fprintf('t_optimum=%f tx_optimum=%f\n',topt,f(topt))
break
end
pause
end
t_optimum=3.819524 tx_optimum=192.677204
f( 3.8195) =192.677204
3
tan1 0.982794 radians ( 56.30993)
2
-which can be used to determine the gradient as f = 0.4i – 1.7j. This corresponds to the direction = tan–
1
(–1.7/0.4) = –1.3397 radians (= –76.76o). This vector as shown below:
2
1.6
1.2
0.8
0.40
0 0.4 0.8 1.2 1.6 2
3x 2.25 y 0
2.25x 4 y 1.75
which can be solved for x = 0.567568 and y = 0.756757, which is the optimal solution.
14.4
(a) Start with an initial guess of x = 1 and y = 1 and apply two applications of the
steepest ascent method to f(x, y) from Prob. 14.3.
(b) Construct a plot from the results of (a) showing the path of the
search.
f
3x 2.25y 3(1) 2.25(1) 0.75
x
f
2.25x 4 y 1.75 2.25(1) 4(1) 1.75 0
y
This can be differentiated and set equal to zero and solved for h* = 0.33333. Therefore, the result for the
first iteration is x = 1 – 0.75(0.3333) = 0.75 and y = 1 + 0(0.3333) = 1.
For the second iteration, the partial derivatives can be evaluated as,
f
3(0.75) 2.25(1) 0
x
f
2.25(0.75) 4(1) 1.75 0.5625
y
This can be differentiated and set equal to zero and solved for h* = 0.25. Therefore, the result for the
second iteration is x = 0.75 + 0(0.25) = 0.75 and y = 1 + (–0.5625)0.25 = 0.859375.
1.2
1 0
1
2
0.8 max
0.6
0.4
0.2
0
0 0.2 0.4 0.6 0.8 1 1.2
14.5 Find the gradient vector and Hessian matrix for each of the following functions:
(a) f(x, y) = 2xy^2 + 3e^xy
(b) f(x, y, z) = x^2 + y^2 + 2z^2
(c) f(x, y) = ln(x^2 + 2xy+ 3y^2)
14.6 Find the minimum value of f (x, y) = (x - 3)^2 + (y - 2)^2 starting at x =1 and y
= 1, using the steepest descent method with a stopping criterion of es = 1%. Explain your
results.
f
2(x 3) 2(1 3) 4
x
f
2( y 2) 2(1 2) 2
y
f (1 4h, 1 2h) (1 4h 3)2 (1 2h 2)2
g(h) (4h 2)2 (2h 1)2
Setting g(h) = 0 gives h* = –0.5. Therefore,
x = 1 –4(–0.5) = 3
y = 1 –2(–0.5) = 2
4.2
3.4
2.6
1.8
1
0
4
0.8
1.6
2.4
3.2
f
4 2x 8x3 2 y 4 2(0) 8(0)3 2(0) 4
x
f
2 2x 6 y 2 2(0) 6(0) 2
y
f(0 + 4h, 0 + 2h) = 20h + 20h2 – 512h4
g(h) = 20 + 40h – 2048h3
The root of this equation can be determined by bisection. Using initial guesses of h = 0 and 1 yields a root
of h* = 0.24390 after 13 iterations with a = 0.05%. Therefore,
x = 0 + 4(0.24390) = 0.976074
y = 0 + 2(0.24390) = 0.488037
14.8 Perform one iteration of the optimal gradient steepest descent method to
locate the minimum of
f (x, y)=-8x + x^2 + 12y + 4y^2 - 2x using initial guesses x = 0 and y = 0.
14.11 Develop a one-dimensional equation in the pressure gradient direction at the point
(4, 2). The pressure function is: f (x, y= 6x^2y - 9y^2 - 8x^2
f (x, y) 2x3 y2 7 yx x2 3y
f
6x2 y2 7 y 2x 6(1)(1) 7(1) 2(1) 1
x
f
4x3y 7x 3 4(1)(1) 7(1) 3 0
x
f 1iˆ 0 ˆj
f f
f (x h, y h) f (1 h, 1 0h)
o
x o
y
2(1 h)3(1)2 7(1 h)(1) (1 h)2 3(1)
xa xb s1 s2 s3 result
-45 -20 0 0 0 0
0.04 0.12 1 0 0 9500
1 1 0 1 0 40
-54 -20 0 0 1 550
xa xb s1 s2 s3 result
1 0 1.6667 0 11.667 p= 22,550
0 1 0.0667 1 -0.3333 xa= 450
0 0 0.0053 0 -0.1466 10
0 0 -0.0667 0 1.3333 xb= 100
Max z=150x1+175x2+250x3
7x1+11x2+15x3<=154
10x1+8x2+12x3<=80
X1<=9
X2<=6
X3<=5
x1 x2 x3 s1 s2 s3 s4 s5 result
-150 -175 -250 0 0 0 0 0 0
7 11 15 1 0 0 0 0 154
10 8 12 0 1 0 0 0 80
1 0 0 0 0 1 0 0 9
0 1 0 0 0 0 1 0 6
0 0 1 0 0 0 0 1 5
x1 x2 x3 s1 s2 s3 s4 s5 result
58.333 0 0 0 20.83 0 8.333 0 z= 1716.7
-5.5 0 0 1 -1.25 0 -1 0 48
0 1 0 0 0 0 1 0 x2 =6
1 0 0 0 0 1 0 0 9
-0.833 0 0 0 -0.083 0 0.67 1 2.333
0.833 0 1 0 0.083 0 -0.67 0 x3 = 2.667
clc
clear all
f=@(x,y) x+y-(x*y);
h=0.1;
x=0:0.1:0.5;
y(1)=1;
n=length(x);
for i = 1:n-1
y(i+1)=
y(i)+(f(x(i),y(i)).*h)+
(f1(x(i),y(i))*((h^2) /2))+
(f2(x(i),y(i)).*((h^3)./6))+
(f3(x(i),y(i)).*((h^4)./24))+
(f4(x(i),y(i)).*((h^5)./120)) ;
fprintf('y(%0.2f)=%0.5f\n',x(i+1),y(i+1))
end
y(0.10)=1.10482
y(0.20)=1.21844
y(0.30)=1.33927
y(0.40)=1.46538
y(0.50)=1.59451
clc
clear all
f=@(x,y) x+y-(x*y);
for i = 1:n-1
y(i+1) =
y(i)+(f(x(i),y(i)).*h)+(f1(x(i),y(i)).*((h^2)./2)
)+(f2(x(i),y(i)).*((h^3)./6))+(f3(x(i),y(i)).*((h
^4)./24))+(f4(x(i),y(i)).*((h^5)./120))+(f5(x(i),
y(i)).*((h^6)./720))+(f6(x(i),y(i)).*((h^7)./5040
)) ;
fprintf('y(%0.2f)=%0.4f\n',x(i+1),y(i+1))
end
y(0.10)=1.1048
y(0.20)=1.2184
y(0.30)=1.3392
y(0.40)=1.4652
y(0.50)=1.5943
clc
clear all
f=@(x,y) (1);
f1=@(x,y) (x/y);
f2=@(x,y) (y-(x*(1)))/(y^2);
f3=@(x,y) ((2*(x*(1)^2))-
(y*((x*(x/y))+(2*1)))/(y^3));
f4=@(x,y) ((-6*x*(1)^3-(y^2)*((x*((y-
(x*(1)))/(y^2)))+(3*(x/y)))+(6*y*1)*((x*(x/y))+1))/(y
^4));
h=0.1;
x=0:0.1:0.5;
y(1)=1;
n=length(x);
for i = 1:n-1
y(i+1) =
y(i)+(f(x(i),y(i)).*h)+(f1(x(i),y(i)).*((h^2)./2))+(f
2(x(i),y(i)).*((h^3)./6))+(f3(x(i),y(i)).*((h^4)./24)
)+(f4(x(i),y(i)).*((h^5)./120));
fprintf('y(%0.2f)=%0.5f',x(i+1),y(i+1))
end
y(0.10)=1.10016 y(0.20)=1.20075 y(0.30)=1.30169 y(0.40)=1.40294 y(0.50)=1.50445
clc
clear
f1=@(x,y) (x+y-(x*y));
y(1)=1;
h=0.001;
x=0:0.001:0.5;
n=length(x);
for i=1:n-1
y(i+1)=y(i)+(h*(f1(x(i),y(i))));
fprintf('y(%0.3f)=(%0.3f)\n',x(i+1),y(i+1))
end
i=(500)
y(0.500)=1.594
clc
clear
f1=@(x,y) (x+y-(x*y));
y(1)=1;
h=0.001;
x=0:0.001:0.5;
n=length(x);
for i=1:n-1
i
y(i+1)=y(i)+(h*(f1(x(i),y(i))));
fprintf('y(%0.3f)=(%0.5f)\n',x(i+1),y(i+1))
end
h=0.001
500 i=
)1.59402(y(0.005)=
clc
clear
fy=@(x,y) (x+y-(x*y));
y(1)=1;
h=0.1;
x=0:0.1:0.5;
n=length(x);
p(1)=y(1);
for i=1:n-1
p(i+1)=y(i)+h*(fy(x(i),y(i)));
y(i+1)=y(i)+(h/2)*(fy(x(i),y(i))+(fy(x(i+1),p(i+1))))
;
fprintf('y(%0.2f)=%0.5f\n',x(i+1),y(i+1))
end
y(0.10)=1.10450
y(0.20)=1.21776
y(0.30)=1.33820
y(0.40)=1.46389
y(0.50)=1.59260
clc
clear
fy=@(t,y) (y^2)+(t^2);
y(1)=0;
h=0.1;
t=1:0.1:2;
n=length(t);
for i=1:n-1
p(i+1)=y(i)+h*(fy(t(i),y(i)));
y(i+1)=y(i)+(h/2)*(fy(t(i),y(i))+fy(t(i+1),p(i+1)));
fprintf('y(%0.2f)=%0.5f\n',t(i+1),y(i+1))
end
h=0.1
y(1.10)=0.11100
y(1.20)=0.24684
y(1.30)=0.41426
y(1.40)=0.62337
y(1.50)=0.89012
y(1.60)=1.24156
y(1.70)=1.72754
y(1.80)=2.45122
y(1.90)=3.66404
y(2.00)=6.15633
h=0.05
y(1.05)=0.05263
y(1.10)=0.11080
y(1.15)=0.17516
y(1.20)=0.24646
y(1.25)=0.32562
y(1.30)=0.41377
y(1.35)=0.51229
y(1.40)=0.62291
y(1.45)=0.74788
y(1.50)=0.89007
y(1.55)=1.05335
y(1.60)=1.24291
y(1.65)=1.46602
y(1.70)=1.73313
y(1.75)=2.05983
y(1.80)=2.47050
y(1.85)=3.00539
y(1.90)=3.73610
y(1.95)=4.80271
y(2.00)=6.51880
clc
clear
fy=@(x,y) (sin(x)+y);
y(1)=2;
h=0.001;
x=0:0.001:0.5;
n=length(x);
for i=1:n-1
p(i+1)=y(i)+h*(fy(x(i),y(i)));
y(i+1)=y(i)+(h/2)*(fy(x(i),y(i))+fy(x(i+1),p(i+1)));
fprintf('y(%0.3f)=%0.5f\n',x(i+1),y(i+1))
end
y (500)= 3.44330
Establish the equation of skydiver’s is
Dv/dt=32.2-cv^(3/2)
c is constant
v is maximum rate of full is 80mps or 117.33
when zero
dv/dt=0
32.2-c(117.33)^(3/2)
C=(32.2/1270.90)=0.025335
The equation is
Dv/dt=32.2-0.025335v^(3/2)
clc
clear
f=@(t,v)32.2-(0.025335*v^(3/2));
h=0.01;
v(1)=0;
t=0:0.2:1;
p(1)=v(1);
n=length(t);
for i=1:n-1
p(i+1)=v(i)+h*f(t(i),v(i));
y(i+1)=v(i)+(h/2)*(f(t(i),v(i))+f(t(i+1),p(i+1)))
;
fprintf('v(%0.2f)=%0.5f’,t(i+1),v(i+1))
end
y(0.20)=0.32198 y(0.40)=0.64389
y(0.60)=0.96570 y(0.80)=1.28740
y(1.00)=1.60895
clc
clear
fy=@(t,y) (y^2)+(t^2);
y(1)=0;
h=0.1;
t=1:0.1:2;
n=length(t);
for i=1:n-1
y(i+1)=y(i)+h*(fy(t(i)+(h/2),y(i)+(h/2)*fy(t(i)+(h/2)
,y(i))));
fprintf('y(%0.2f)=%0.5f',t(i+1),y(i+1))
end
y(1.10)=0.11055 y(1.20)=0.24595 y(1.30)=0.41290 y(1.40)=0.62142 y(1.50)=0.88730
y(1.60)=1.23712 y(1.70)=1.71956 y(1.80)=2.43406 y(1.90)=3.61813 y(2.00)=5.99003
clc
clear
f=@(x,y) (x+y-(x*y));
y(1)=1;
h=0.1;
x=0:0.1:1;
n=length(x);
for i=1:n-1
k1(i)=h*f(x(i),y(i));
k2(i)=h*f(x(i)+(h/2),y(i)+(k1(i)/2));
k3(i)=h*f(x(i)+(h/2),y(i)+(k2(i)/2));
k4(i)=h*f(x(i)+h,y(i)+k3(i));
y(i+1)=y(i)+(1/6)*(k1(i)+2*k2(i)+2*k3(i)+k4(i));
fprintf('y(%0.2f)=%0.5f',x(i+1),y(i+1))
end
y(0.10)=1.10482 y(0.20)=1.21840 y(0.30)=1.33917 y(0.40)=1.46518 y(0.50)=1.59420
y(0.60)=1.72375 y(0.70)=1.85122 y(0.80)=1.97394 y(0.90)=2.08933 y(1.00)=2.19496
clc
clear
f=@(x,y) 1/(x+y);
y(1)=2;
h=0.2;
x=0:0.2:1;
n=length(x);
for i=1:n-1
k1(i)=h*f(x(i),y(i));
k2(i)=h*f(x(i)+(h/2),y(i)+(k1(i)/2));
k3(i)=h*f(x(i)+(h/2),y(i)+(k2(i)/2));
k4(i)=h*f(x(i)+h,y(i)+k3(i));
y(i+1)=y(i)+(1/6)*(k1(i)+2*k2(i)+2*k3(i)+k4(i));
fprintf('y(%0.2f)=%0.5f\n',x(i+1),y(i+1))
end
y(0.20)=2.09327
y(0.40)=2.17549
y(0.60)=2.24927
y(0.80)=2.31636
y(1.00)=2.37797
clc
clear
fx=@(t,x,y) ((x*y)-t);
fy=@(t,x,y) (x+t);
x(1)=1;
y(1)=0;
h=0.2;
t=0:0.2:1;
n=length(t);
for i=1:n-1
p(i+1)=x(i)+h*(fx(t(i),x(i),y(i)));
c(i+1)=y(i)+h*(fy(t(i),x(i),y(i)));
y(i+1)=y(i)+(h/2)*(fy(t(i),x(i),y(i))+fy(t(i+1),p(i+1
),c(i+1)));
x(i+1)=x(i)+(h/2)*(fx(t(i),x(i),y(i))+fx(t(i+1),p(i+1
),c(i+1)));
fprintf('y(%0.2f)=%0.5f\n',t(i+1),y(i+1))
fprintf('x(%0.2f)=%0.5f\n',t(i+1),x(i+1))
end
y(0.20)=0.22000
x(0.20)=1.00000
y(0.40)=0.48040
x(0.40)=1.00818
y(0.60)=0.78372
x(0.60)=1.03473
y(0.80)=1.13489
x(0.80)=1.09543
y(1.00)=1.54284
x(1.00)=1.21902
b
Clc
clear
f=@(t,y)(y*t^3)-(1.5*y);
h=0.5;
y(1)=1;
t=0:0.5:2;
p(1)=y(1);
n=length(t);
for i=1:n-1
p(i+1)=y(i)+h*f(t(i),y(i));
y(i+1)=y(i)+(h/2)*(f(t(i),y(i))+f(t(i+1),p(i+1)))
;
fprintf('y(%0.2f)=%0.5f\n',t(i+1),y(i+1))
end
command window when h=0.5
y(0.50)=0.53323
y(1.00)=0.32326
y(1.50)=0.40480
y(2.00)=1.93961
command window when h=0.1
y(0.75)=0.25795
y(1.00)=0.18842
y(1.25)=0.16487
y(1.50)=0.18355
y(1.75)=0.26959
y(2.00)=0.52969
c.
clc
clear
f=@(t,y)(y*t^3)-(1.5*y);
y(1)=1;
t=0:0.5:2;
h=0.5;
n=length(t);
for i=1:n-1
y(i+1)=y(i)+(h).*(f(t(i)+(h/2),y(i)+(h/2)*f(t(i)+
(h/2),y(i))));
fprintf('y(%0.2f)=%0.5f\n',t(i+1),y(i+1))
end
commend window
y(0.50)=0.53613 y(1.00)=0.34670 y(1.50)=0.41416 y(2.00)=1.51951
d.
clc
clear
f=@(t,y)(y*t^3)-(1.5*y);
t=0:0.25:1;
h=0.25;
y(1)=1;
n=length(t);
for i=1:n-1
k1(i)=h*f(t(i),y(i));
k2(i)=h*(f(t(i)+(h/2),y(i)+(k1(i)/2)));
k3(i)=h*(f(t(i)+(h/2),y(i)+(k2(i)/2)));
k4(i)=h*(f(t(i)+h,y(i)+k3(i)));
y(i+1)=y(i)+(1/6)*(k1(i)+(2*k2(i))+(2*k3(i))+k4(i
));
fprintf('y(%0.2f)=%0.5f\n',t(i+1),y(i+1))
end
commend window
y(0.50)=0.68802
y(1.00)=0.50386
y(1.50)=0.49653
y(2.00)=1.00264
B
clc
clear
f1=@(x,y) (1+(4*x))*(sqrt(y));
y(1)=1;
h=0.25;
x=0:0.25:1;
n=length(x);
for i=1:n-1
y(i+1)=y(i)+(h*(f1(x(i),y(i))));
fprintf('y(%0.2f)=(%0.5f)\n',x(i+1),y(i+1))
end
=y (0.25) )1.25000
y(0.50))1.80902(=
y(0.75))2.81776(=
y(1.00))4.49638(=
e
clc
clear
f=@(x,y) (1+(4*x))*(sqrt(y));
y(1)=1;
h=0.25;
x=0:0.25:1;
n=length(x);
for i=1:n-1
k1(i)=h*f(x(i),y(i));
k2(i)=h*f(x(i)+(h/2),y(i)+(k1(i)/2));
k3(i)=h*f(x(i)+(h/2),y(i)+(k2(i)/2));
k4(i)=h*f(x(i)+h,y(i)+k3(i));
y(i+1)=y(i)+(1/6)*(k1(i)+2*k2(i)+2*k3(i)+k4(i));
fprintf('y(%0.2f)=%0.5f\n',x(i+1),y(i+1))
end
y(0.25)=1.41009 y(0.50)=2.24979 y(0.75)=3.75341 y(1.00)=6.24905
(c)
clc
clear
f=@(t,y)(-2*y)+(t^2);;
y(1)=1;
t=0:0.5:2;
h=0.5;
n=length(t);
for i=1:n-1
y(i+1)=y(i)+(h).*(f(t(i)+(h/2),y(i)+(h/2)*f(t(i)+
(h/2),y(i))));
fprintf('y(%0.2f)=%0.5f',t(i+1),y(i+1))
end
y(0.50)=0.51563 y(1.00)=0.39844 y(1.50)=0.58984 y(2.00)=1.06055
A
clc
clear
fy=@(t,y,z) (-2*y)+(5*exp(-t));
fz=@(t,y,z) ((y*(z^2))/-2);
y(1)=2;
z(1)=4;
h=0.1;
t=0:0.1:0.4;
n=length(t);
for i=1:n-1
y(i+1)=y(i)+(h*(fy(t(i),y(i),z(i))));
z(i+1)=z(i)+(h*(fz(t(i),y(i),z(i))));
fprintf('y(%0.2f)=(%0.5f)\n',t(i+1),y(i+1))
fprintf('z(%0.2f)=(%0.5f)\n',t(i+1),z(i+1))
end
y(0.10)=2.10000 z(0.10)=2.40000
y(0.20)=2.13242 z(0.20)=1.79520
y(0.30)=2.11530 z(0.30)=1.45159
y(0.40)=2.06265 z(0.40)=1.22873
b
clc
clear all
fy=@(t,y,z) ((-2*y)+5*exp(-t));
fz=@(t,y,z)((-y*(z^2)/2));
y(1)=2;
z(1)=4;
h=0.1;
t=0:0.1:0.4;
n=length(t);
for i=1:n-1
k1(i)=h*fz(t(i),y(i),z(i));
m1(i)=h*fy(t(i),y(i),z(i));
k2(i)=h*fz(t(i)+(h/2),y(i)+(m1(i)/2),z(i)+(k1(i)/2));
m2(i)=h*fy(t(i)+(h/2),y(i)+(m1(i)/2),z(i)+(k1(i)/2));
k3(i)=h*fz(t(i)+(h/2),y(i)+(m2(i)/2),z(i)+(k2(i)/2));
m3(i)=h*fy(t(i)+(h/2),y(i)+(m2(i)/2),z(i)+(k2(i)/2));
k4(i)=h*fz(t(i)+h,y(i)+m3(i),z(i)+k3(i));
m4(i)=h*fy(t(i)+h,y(i)+m3(i),z(i)+k3(i));
z(i+1)=z(i)+(1/6)*(k1(i)+2*k2(i)+2*k3(i)+k4(i));
y(i+1)=y(i)+(1/6)*(m1(i)+2*m2(i)+2*m3(i)+m4(i));
fprintf('z(%0.2f)=%0.5f\n',t(i+1),z(i+1))
fprintf('y(%0.2f)=%0.5f\n',t(i+1),y(i+1))
end
z(0.10)=2.84176
y(0.10)=2.06799
z(0.20)=2.19379
y(0.20)=2.08269
z(0.30)=1.78739
y(0.30)=2.05765
z(0.40)=1.51262
y(0.40)=2.00361
clc
clear
fy=@(t,y,u) u;
fu=@(t,y,u) (((1-(y^2))*u)-y);
y(1)=1;
u(1)=1;
h=0.25; %0.125
t=0:0.25:10;
n=length(t);
for i=1:n-1
y(i+1)=y(i)+h*fy(t(i),y(i),u(i));
u(i+1)=u(i)+h*fu(t(i),y(i),u(i));
fprintf('y(%0.2f)=%0.5f\n',t(i+1),y(i+1))
fprintf('u(%0.2f)=%0.5f\n',t(i+1),u(i+1))
end
plot(x,y);
h=0.25
y(10.00)=1.52020
u(10.00)=-0.80188
h=0.125
y(10.00)=-1.35609
u(10.00)=0.90269
a
clc
clear
fy=@(t,y,u) u;
fu=@(t,y,u) -4*y;
y(1)=1;
u(1)=0;
h=0.1;
t=0:0.1:4;
n=length(t);
for i=1:n-1
y(i+1)=y(i)+h*fy(t(i),y(i),u(i));
u(i+1)=u(i)+h*fu(t(i),y(i),u(i));
fprintf('y(%0.2f)=%0.5f\n',t(i+1),y(i+1))
fprintf('u(%0.2f)=%0.5f\n',t(i+1),u(i+1))
end
y(4.00)=-0.09165
u(4.00)=-4.37841