Calculating the height of a liquid in a sphere using secant method
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Justin Michaellmore
el 13 de En. de 2021
Editada: James Tursa
el 14 de En. de 2021
The problem is to determine how much the spherical tank must be filled to contain 30m3 if
R = 3m. The volume is calculated according to equation: V = (𝜋ℎ^2*[3𝑅 − ℎ])/3
Use the secant method. Print the approximations, the relative error. Ensure that the calculation ends when the error is less than a certain value.
Find the correct solutions using the appropriate matlab function. Print approximations and errors.
Bonus: show graphically the dependance of the Volume V on the Height h. f(x).
2 comentarios
James Tursa
el 14 de En. de 2021
What have you done so far? What specific problems are you having with your code?
Respuesta aceptada
James Tursa
el 14 de En. de 2021
Editada: James Tursa
el 14 de En. de 2021
You need to use a function that is 0 at the h you are looking for. So you need that 30 in your function handle. E.g.,
func = @(h)(pi*h^2*(9-h))/3 - 30
If you want to compare to a MATLAB function answer, realize that you have a polynomial in h. So just make a vector of the coefficients and feed it to the roots( ) function.
4 comentarios
James Tursa
el 14 de En. de 2021
Editada: James Tursa
el 14 de En. de 2021
"Didn't work for me"
So, that doesn't tell me much. Running your exact code with the -30 addition produced the answer for me. What is it that didn't work for you? You should be getting the exact same answer I did.
clear all
clc
er=100;
es=0.001;
func = @(h)(pi*h^2*(9-h))/3 - 30;
h1=1;
h2=2;
f1=func(h1);
counter=0;
while abs(er) > es
counter=counter+1;
f2=func(h2);
er=((h2-h1)*f2)/(f2-f1);
fprintf('error:%f\n',er);
h1=h2;
f1=f2;
h2=h2-er;
fprintf('height (aprox):%f\n',h2);
end
As for the plot, just realize that the height can range from 0 to 2*R. So create that range and plug it into your vectorized V formula:
R = 3;
h = 0:0.01:2*R;
V = (pi*h.^2.*(9-h))/3;
Then plot(h,V)
Más respuestas (0)
Ver también
Categorías
Más información sobre Red en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!