1
Is asked to resolve m* d 2 y/dt=-g-gamma*dy/dt such that y(20)=0, with the initial conditions I’ve already put in the code.
What is wrong?
%3.
[value,x0,numIter] = f_secante(@find0_p3,0, 1, 0.0001, 1000);
%[x,v]
m=0.01; g=9.8; gama=0.05;
f=@(x,t)[x(2); -(g*m+gama*x(2))/m];
x=[x0;0.3];t=0;tf=20; dt=0.01;
t=0;
time=[t];
mat=[x];
while t<tf
[t,x] = f_rk4(f,t,x,dt);
time=[time,t];
mat=[mat,x];
end
plot(time,mat(1,:))
fprintf('%s %f\n','y(0)=',x0)
With the function:
function [value,x0,numIter] = f_secante(f, a, b, tol, nmax)
%[value,x0,numIter] = f_secante(f, a, b, tol, nmax)
if f(a)==0
x0=a; numIter=0; value=0;
return;
end;
if f(b)==0
x0=b; numIter=0; value=0;
return;
end;
x1=a; x2=b;
for k=1:nmax
x3=x2-(((x2-x1)*f(x2))/(f(x2)-f(x1)));
if abs((x3-x2)/x2)<tol; x0=x3; numIter=k;
continue;
else
x1=x2;x2=x3;
end
if f(x3)~=0 ; x0 = a; numIter=k; value=f(x3);
return
end
end
And the second Function:
function z=find0_p3(y0)
%[x,v]
m=0.01; g=9.8; gama=0.05;
f=@(x,t)[x(2); -(g*m+gama*x(2))/m];
x=[y0;0.3];t=0;tf=20; dt=0.01;
while t<tf
[t,x] = f_rk4(f,t,x,dt);
end
z=x(1);
end
Can you help me?