Error using function matlab

11 vues (au cours des 30 derniers jours)
Nikki Dobson
Nikki Dobson le 9 Nov 2020
Commenté : Nikki Dobson le 9 Nov 2020
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 commentaire
Walter Roberson
Walter Roberson le 9 Nov 2020
You do not call the function bisection

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 9 Nov 2020
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 commentaire
Nikki Dobson
Nikki Dobson le 9 Nov 2020
thanks for your help, it worked :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by