MATLAB: Error in the function

functionsmatlab function

This my code. I don't know what is wrong with my code as it does not give any outputs when you run it. so can someone please help me? thank you
clear;
clc;
function calculate
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
%calling function
bisection(@f);
%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
end
function bisection(f)
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
In addtion how would i plot displacement x(t) between 0<t<6s using the driving frequenxy produced from the above code?

Best Answer

clear;
clc;
Get rid of these. As written your file is a script file with local functions, and the only code that is executed when you run this script are these two lines. If you start your file with the "function calculate" line, it becomes a function file and calling it will call the calculate function.
function calculate
If you want your calculate function to return anything you need to define it to return something. As written calculate takes no input arguments and returns no output arguments. See this documentation page for how to define a function to return one or more output arguments.
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
%calling function
bisection(@f);
bisection accepts one input argument and returns no output arguments. You need to define it to return an output then call it to assign that output to something in order to get it to work the way you described wanting it to work.
I snipped the rest of the code because I didn't see anything at a quick glance that needed modification. [There was at least one typo in the comments, but that doesn't affect the results of the code.]