Bisection help PLS HELP ME

13 ビュー (過去 30 日間)
Triggs
Triggs 2019 年 7 月 19 日
編集済み: Triggs 2019 年 8 月 19 日
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

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 19 日
編集済み: KALYAN ACHARJYA 2019 年 7 月 19 日
"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 件のコメント
madhan ravi
madhan ravi 2019 年 7 月 19 日
There are some unnecessary lines of code.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by