Finding the zeros of two functions

4 views (last 30 days)
Jonas Andersson
Jonas Andersson on 25 Mar 2024
Edited: Drishti on 19 Sep 2024
Hello,
I am trying to balance a ship in water and for that I need to find the value where the ship is balanced in the water.
I am first using fzero to balance the ship in the Z direction (up and down) and then I am using fzero again to balance it in rotation around the Y axis (pitch). Since the boyancy of the ship changes when the pitch is changed I would like to be able to do fzero in the Z direction and around the Y axis.
Is there a way to find the zeros of both these variables?
Thank you in advance!
% This is where the fzeros are used
% eta3 is the Z coordinate and eta5 is the rotation around the Y axis.
eta3start = -1;
[eta3] = fzero(@(eta3) ForceVertical(eta3,ship,state),eta3start);
state.eta(3) = eta3;
eta5start = 0;
[eta5] = fzero(@(eta5) MomentVertical(eta5,ship,state), eta5start);
state.eta(5) = eta5;
% This is how ForceVertical and MomentVertical look
function [F3] = ForceVertical(eta3,ship,state)
state.eta(3) = eta3;
[HS] = CalculateHydrostatics(ship,state);
FB = HS.FB0(3);
FG = ship.m * state.g * -1; % equal to F G in equation (11) in Rosén (2017) – remember the minus sign!;
F3 = FB + FG; % according to (12)&(13) in Rosén (2017)
end
function [M3] = MomentVertical(eta5,ship,state)
state.eta(5) = eta5;
[HS] = CalculateHydrostatics(ship,state);
M3 = cosd(eta5)*(-HS.CoB(1)+ship.CoG(1));
end
  2 Comments
Davide Masiello
Davide Masiello on 25 Mar 2024
It seems you might be dealing with a system of non-linear equations.
To solve this kind of problem, you could use fsolve.
Sam Chak
Sam Chak on 25 Mar 2024
Based on the code you provided, it appears that you are attempting to find the equilibrium point for a marine vessel in water. To begin, would it be possible for you to utilize a trial-and-error approach in order to identify solution sets that are relatively close to zero? This could help narrow down the search for the equilibrium point.

Sign in to comment.

Answers (1)

Drishti
Drishti on 19 Sep 2024
Edited: Drishti on 19 Sep 2024
Hi Jonas,
To utilize the MATLAB ‘fzero’ function to find the balanced point, you need to consider the convergence of both ‘eta3’ representing vertical displacement and ‘eta5’ representing pitch angle of the ship.
While utilizing the ‘fzero’ function, it is necessary to use iterative approach to further enhance the solution.
You can refer to the following code snippet to check the convergence of both the variables to find the balanced point:
%upto number of iterations you want to run this section
while ~converged && iteration < maxIterations
iteration = iteration + 1;
% Solve for eta3
[eta3] = fzero(@(eta3) ForceVertical(eta3, ship, state), eta3start);
state.eta(3) = eta3;
% Solve for eta5
[eta5] = fzero(@(eta5) MomentVertical(eta5, ship, state), eta5start);
state.eta(5) = eta5;
% Check for convergence
if abs(eta3 - eta3start) < tolerance && abs(eta5 - eta5start) < tolerance
converged = true;
else
% Update starting points for the next iteration
eta3start = eta3;
eta5start = eta5;
end
end
Furthermore, considering the interaction between vertical displacement and pitch resulting from changes in buoyancy, another workaround would be to use ‘fsolve’ function to find equilibrium in both the Z direction and around the Y-axis.
To utilize ‘fsolve’ function, you can refer to the below given code:
% Solve the system of equations
[solution, fval, exitflag, output] = fsolve(@(vars) BalanceEquations(vars, ship, state), initialGuess, options);
% Extract the solutions
eta3 = solution(1);
eta5 = solution(2);
% Update the state with the found solutions
state.eta(3) = eta3;
state.eta(5) = eta5;
For more information, you can refer to the MATLAB documentation of the ‘fsolve’ and ‘fzero’ functions:
I hope this resolves the issue.

Categories

Find more on Function Creation in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!