Matlab help in incremental methods
Mostrar comentarios más antiguos
Does this code look correct for following problem? I was told there was an issue in it, teacher told me to find it rather than show me.
clc;
clear all;
%%Incremental Method
g = 9.81;
v = 5;
t = 3;
L = 5;
h = 0; %initial guess of height
dh = 0.01; %incremental value
f1 = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h)); %initial error
h = h+dh;
f2 = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h));
while f1*f2 > 0 % check whether solution exists or not
f1 = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h));
h = h+dh;
f2 = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h));
end
disp(h); % result height using incremental method
%%Bisection Method
g = 9.81;
v = 5; % 2nd question part a
t = 3;
L = 5;
syms h
f(h) = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h));
a = 0 ; %inital guesses of height
b = 5; %inital guesses of height
if f(a)*f(b)>0
disp('Wrong choice')
else
p = (a + b)/2; %bisection algorithm
err = abs(f(p));
while err > 1e-3
if f(a)*f(p)<0
b = p;
else
a = p;
end
p = (a + b)/2;
err = abs(f(p));
end
end
disp(a) % result height using bisection method
v = 5; % 2nd question part b
t = 3;
L = 10;
syms h
f(h) = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h));
a = 0 ; %inital guesses of height
b = 5; %inital guesses of height
if f(a)*f(b)>0
disp('Wrong choice')
else
p = (a + b)/2; %bisection algorithm
err = abs(f(p));
while err > 1e-3
if f(a)*f(p)<0
b = p;
else
a = p;
end
p = (a + b)/2;
err = abs(f(p));
end
end
disp(a) % result height using bisection method
v = 10; % 2nd question part c
t = 2;
L = 10;
syms h
f(h) = v - sqrt(2.*g.*h)*tanh((t./(2*L))*sqrt(2.*g.*h));
a = 0 ; %inital guesses of height
b = 10; %inital guesses of height
if f(a)*f(b)>0
disp('Wrong choice')
else
p = (a + b)/2; %bisection algorithm
err = abs(f(p));
while err > 1e-3
if f(a)*f(p)<0
b = p;
else
a = p;
end
p = (a + b)/2;
err = abs(f(p));
end
end
disp(a) % result height using bisection method
Results
1.4900 m
1.4844 m
2.2192 m
7.3340 m

Respuestas (0)
Categorías
Más información sobre Symbolic Math Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!