Filter löschen
Filter löschen

Bisection Method Table Generation Error - Array Indices Issue

5 Ansichten (letzte 30 Tage)
Teoman
Teoman am 22 Sep. 2023
Beantwortet: Dyuman Joshi am 22 Sep. 2023
I'm trying to create a MATLAB code that dynamically generates a table using the bisection method to estimate the drag coefficient of a bungee jumper. I have two files: `test.m` and `bisection_solver.m`. However, when I run the `test.m` script, I encounter an error that I can't seem to resolve. Here are the relevant code snippets:
File 1: test.m:
% Script 2
velocity_target = 46; % Target velocity (m/s)
time = 9; % Time of free fall (s)
g = 9.81; % Acceleration due to gravity (m/s^2)
mass = 95; % Mass of the bungee jumper (kg)
% Initial guesses and error criterion
xl = 0.2;
xu = 0.5;
error_criterion = 0.001;
% Initialize variables
Cd = (xl + xu) / 2; % Initial estimate of Cd
relative_error = 1; % Initialize with a value greater than the error criterion
% Define another mathematical expression as a function handle
expression = @(Cd) sqrt((mass * g) / Cd) * tanh(sqrt((Cd * g) / mass) * time);
% Call the bisection_solver function
[root, iterations] = bisection_solver(expression, xl, xu, error_criterion, Cd, relative_error);
fprintf('The root of the equation is %.6f after %d iterations.\n', root, iterations);
File 2: bisection_solver.m
function [root, iterations] = bisection_solver(expression, xl, xu, error_criterion, Cd, relative_error)
fprintf('Iteration\tLower Bound\tUpper Bound\tEstimated Cd\tRelative Error\n');
iteration = 0;
while relative_error > error_criterion
iteration = iteration + 1;
% Calculate the velocity using the current estimate of Cd
velocity_estimated = expression(Cd);
% Check if the estimated velocity is greater or smaller than the target
if velocity_estimated > velocity_target
xl = Cd; % Update the lower bound
relative_error = abs((xl - xu) / xl);
else
xu = Cd; % Update the upper bound
relative_error = abs((xu - xl) / xu);
end
if iteration > 1
% Print the current iteration information
fprintf('%d\t%.6f\t%.6f\t%.6f\t%.6f\n', iteration, xl, xu, Cd, relative_error);
else
fprintf('%d\t%.6f\t%.6f\t%.6f\t--\n', iteration, xl, xu, Cd);
end
% Calculate the new estimate of Cd using bisection
Cd = (xl + xu) / 2;
end
fprintf('Hence, after the %d th iteration, the relative error is below %.2f%%.\n',iteration, relative_error * 100);
fprintf('The final estimated drag coefficient is %.6f.\n', Cd);
end
When I run `test.m`, I get the following error message:
Iteration Lower Bound Upper Bound Estimated Cd Relative Error
Array indices must be positive integers or logical values.
Error in bisection_solver (line 13)
velocity_estimated = expression(Cd);
Expected Answer :
Iteration Lower Bound Upper Bound Estimated Cd Relative Error
1 0.350000 0.500000 0.350000 --
2 0.350000 0.425000 0.425000 0.176471
3 0.387500 0.425000 0.387500 0.096774
4 0.387500 0.406250 0.406250 0.046154
Hence, after the 4 th iteration, the relative error is below 4.62%.
The final estimated drag coefficient is 0.046154.
I'm not sure what's causing this error. Can someone help me identify and fix the issue so that I can successfully use the bisection method to estimate the drag coefficient and create the table?

Antworten (1)

Dyuman Joshi
Dyuman Joshi am 22 Sep. 2023
You have
> not passed the variable velocity_target to the function
> not defined the output root in the function
Make the appropriate changes and the function should work

Community Treasure Hunt

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

Start Hunting!

Translated by