Clear Filters
Clear Filters

Bisection help PLS HELP ME

1 view (last 30 days)
Triggs
Triggs on 19 Jul 2019
Edited: Triggs on 19 Aug 2019
I have this code for a bisection method but I cant seem to figure out why it wont work I just get this error
every thing else is given
any help is appreciated thanks
Error using *
Incorrect dimensions for matrix multiplication. Check that the
number of columns in the first matrix matches the number of rows in
the second matrix. To perform elementwise multiplication, use '.*'.
Error in bisectiona>f (line 65)
x=(2*Fo/wn.^2-w.^2)*sin((wn-w/2)*t)*sin((wn+w/2)*t);
Error in bisectiona (line 14)
f_left = f(x_left);
function bisectiona
% Bisection method: Used for solving an equation, and finding an
% approximate solution to an equation.
clc
% The number of bisection steps
n = 32;
% define the initial interval
a = 41;
b = 69;
fprintf('\n initial interval [%g, %g] \n total bisection steps %d\n', a,b,n);
% Check that a root does exist in the chosen interval
x_left = a;
x_right = b;
f_left = f(x_left);
f_right = f(x_right);
if f_left*f_right > 0
end
% Bisection: The method
for i=1:n
if f_left == 0
% The exact root of the equation is found at the lower bound
% of the chosen interval
fprintf('\n stage %g root %g with zero absolute error \n',i,x_left);
return;
end
if f_right==0
% The exact root of the equation is found at the upper bound
% of the chosen interval
fprintf('\n stage %g root %g with zero absolute error \n',i,x_right);
return
end
% Bisection method: The process
x_mid = (x_left+x_right)/2.0;
f_mid = f(x_mid);
if f_left*f_mid <= 0
% There is a root in [x_left,x_mid]
x_right = x_mid;
f_right = f_mid;
end
if f_left*f_mid > 0
% There is a root in [x_mid,x_right]
x_left = x_mid;
f_left = f_mid;
end
% Calculate the approximate root for current step
root = (x_left+x_right)/2.0;
% Calculate the absolute error for current step
abs_err=(x_right-x_left)/2.0;
fprintf('\n stage %g root %g absolute error < %g \n',i,root,abs_err);
end
%check satisfaction of equation at end of process
residual = f(root);
fprintf('\n final residual = %g \n',residual);
end
%Created Subfunction to define equation f(x)
function f_value = f(~)
k = 2800;
m = 0.6705;
Fo = 2.0535;
t = 9.177;
x = 0.0253;
w = 77:0.1:80;
wn= sqrt(k/m);
fo= Fo/m;
x=(2*Fo/wn.^2-w.^2)*sin((wn-w/2)*t)*sin((wn+w/2)*t);
f_value = x;
end

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 19 Jul 2019
Edited: KALYAN ACHARJYA on 19 Jul 2019
"but I cant seem to figure out why it wont work I just get this error"
function f_value=f(~)
k = 2800;
m = 0.6705;
Fo = 2.0535;
t = 9.177;
x = 0.0253;
w = 77:0.1:80;
wn= sqrt(k/m);
fo= Fo/m;
x=(2*Fo/wn^2-w.^2).*sin((wn-w./2)*t).*sin((wn+w./2).*t);
f_value = x;
end
Main Script:
clc
clear all;
close all;
% approximate solution to an equation.
% The number of bisection steps
n = 32;
% define the initial interval
a = 41;
b = 69;
fprintf('\n initial interval [%g, %g] \n total bisection steps %d\n', a,b,n);
% Check that a root does exist in the chosen interval
x_left = a;
x_right = b;
f_left=f(x_left);
f_right=f(x_right);
if (f_left.*f_right)> 0
end
% Bisection: The method
for i=1:n
if f_left == 0
% The exact root of the equation is found at the lower bound
% of the chosen interval
fprintf('\n stage %g root %g with zero absolute error \n',i,x_left);
return;
end
if f_right==0
% The exact root of the equation is found at the upper bound
% of the chosen interval
fprintf('\n stage %g root %g with zero absolute error \n',i,x_right);
return
end
% Bisection method: The process
x_mid = (x_left+x_right)/2.0;
f_mid = f(x_mid);
if f_left.*f_mid <= 0
% There is a root in [x_left,x_mid]
x_right = x_mid;
f_right = f_mid;
end
if f_left.*f_mid > 0
% There is a root in [x_mid,x_right]
x_left = x_mid;
f_left = f_mid;
end
% Calculate the approximate root for current step
root = (x_left+x_right)/2.0;
% Calculate the absolute error for current step
abs_err=(x_right-x_left)/2.0;
fprintf('\n stage %g root %g absolute error < %g \n',i,root,abs_err);
end
%check satisfaction of equation at end of process
residual = f(root);
fprintf('\n final residual = %g \n',residual);
Command Window:
pic22.png
Please note: I removed the code errors only.
  1 Comment
madhan ravi
madhan ravi on 19 Jul 2019
There are some unnecessary lines of code.

Sign in to comment.

More Answers (0)

Categories

Find more on Physics in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!