Error using function matlab

조회 수: 16 (최근 30일)
Nikki Dobson
Nikki Dobson 2020년 11월 9일
댓글: Nikki Dobson 2020년 11월 9일
Hi, I’m trying to use the bisection method to find x. Think there’s a problem using the function. If you have any hints to help me fix it, it would be much appreciated. Thanks
clc
k=1007.564; %stiffness (N/m)
m=98.668; %mass (kg)
F_0=108.764; %applied force magnitude (N)
x_0=-0.02; %initial displacement (m)
v_0=0.1; %initial velocity (m/s)
t=4.54; %time (s)
x_1=0.0386; %displacement (m)
om_n=sqrt(k/m);
f_0=F_0/m;
x=0; %omega
function bisection
n = 20;
% initial interval
a = 5.12;
b = 6.45;
fprintf('\n initial interval [%g, %g] \n total bisection steps %d\n', a,b,n);
%initialise and check that there is a root in the prescribed interval
x_left = a;
x_right = b;
f_left = f(x_left);
f_right = f(x_right);
if f_left*f_right > 0
error('ERROR: no root in the specified interval');
end
%the bisection method
for i=1:n
if f_left == 0
%exact root is reached by the left bound of the interval
fprintf('\n stage %g root %g with zero absolute error \n',i,x_left);
return;
end
if f_right==0
%exact root is reached by the right bound of the interval
fprintf('\n stage %g root %g with zero absolute error \n',i,x_right);
return
end
%the bisection 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
%compute the approximate rood for the current bisection step
root = (x_left+x_right)/2.0;
%compute the absolute error for the current bisection 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
%Subfunction defines the equation f(x) = 0
function f_value = f(x) %x=omega
f_value = (v_0/om_n)*sin(om_n*t) + (x_0-((f_0)/(om_n)^2-(x)^2))*cos(om_n*t) + ((f_0)/(om_n)^2-(x)^2)*cos(x*t);
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 11월 9일
You do not call the function bisection

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 11월 9일
You do not call the function bisection
Your function f does not have access to the variables defined in the script. f would need to be a nested function using shared variables -- and you need to nest inside a function, not inside a script.
For example,
function driver
k=1007.564; %stiffness (N/m)
m=98.668; %mass (kg)
F_0=108.764; %applied force magnitude (N)
bisection(@f); %call your function
function f_value = f(x) %x=omega
%this is a nested function that shares variables
f_value = (v_0/om_n)*sin(om_n*t) + (x_0-((f_0)/(om_n)^2-(x)^2))*cos(om_n*t) + ((f_0)/(om_n)^2-(x)^2)*cos(x*t);
end
end
function bisection(f)
stuff
end
  댓글 수: 1
Nikki Dobson
Nikki Dobson 2020년 11월 9일
thanks for your help, it worked :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by