Not sure how to fix error; "Function definitions in a script must appear at end of file". Need them to both work on the same file for publishing purposes.

6 次查看(过去 30 天)
The only time I've used Secant_Method is in problem 1. and wasn't sure how to fix the error MATLAB has given me citing the problem at the beginning of problem 2.
I just need them to be on the same file so I can publish together; they work apart-but not together.
%% 1. Small Object Free-Fall
clear all
clc
%gravity
g=9.81;
%k [kg/s]
k=0.5;
% time [s]
t=20;
% velocity [m/s]
v=100;
% starting mass [kg]
m0=0;
% initial guess for mass [kg]
m1=0.1;
% define function handles for the function
fun_m=@(m)(m*g/k).*(1-exp((-k./m)*t))-v;
% define function handles for the first derivative
fun_mp=@(m) (g /k).*(1-exp((-k./m)*t))-(g*t./m).*exp((-k./m)*t);
% mass values to plot
dm=(m1-m0)/10;
m=m0:dm:120*m1;
m2=fun_m(m);
% graphing
plot(m,m2,'LineWidth',2)
hold on
plot([m0 120*m1],[0 0],'LineWidth',2)
chr3 = ['Velocity of the object at time t = 20 sec'];
title({chr3});
xlabel('mass m (kg)');
ylabel('Velocity -120 (m/s)');
legend('Velocity = 120 line','f(m)','Location','southeast')
ax = gca;
ax.FontSize = 15;
%% Problem 1a.Newton-Raphson Method
guess=0.1;
[root_Newton_Raphson count_NR]=Newton_Raphson_Method(fun_m,fun_mp,guess) ;
%% Problem 1a. Secant Method
guess1=0.0;
guess2=0.1;
[root_Secant_Method count_Sec]=Secant_Method(fun_m,guess1,guess2);
%% Problem 1b. Displaying Output
format long
fprintf('\n\n');
fprintf('--------------------------------------------------------------------------------\n');
fprintf('Mass of the object that attains 100 m/s in 20 sec is \n');
fprintf('By Newton-Raphson Method = %.15f kg Number of itrations used = %d \n',root_Newton_Raphson,count_NR);
fprintf('By Secant Method = %.15f kg Number of itrations used = %d \n',root_Secant_Method,count_Sec);
fprintf('--------------------------------------------------------------------------------\n');
fprintf('\n\n');
function [root_Newton_Raphson count]= Newton_Raphson_Method(f,fp,guess);
% This subroutine finds the root of the function
% It needs function and derivative of the function and a initial guess to be specified
Error_limit = 1e-8;
x=guess ;
iterate=1;
count=0;
while (iterate==1)
count=count+1;
x_new=x-(f(x)/fp(x));
if (abs(x_new-x)<Error_limit)
iterate=0;
else
x=x_new;
end
end
root_Newton_Raphson=x_new ;
end
function [root_Secant_Method count]= Secant_Method(f,guess1,guess2);
% This subroutine finds the root of the function
% It needs function and two initial guesses to be specified
Error_limit=1e-8;
xim1=guess1 ;
xi=guess2 ;
iterate=1;
count=0;
while(iterate == 1)
count=count+1;
x_new=xi-(f(xi)*(xim1-xi)/(f(xim1)-f(xi)));
if(abs(x_new - xi)< Error_limit)
iterate=0;
else
xim1=xi ;
xi=x_new;
end
end
root_Secant_Method=x_new;
end
%% Problem 2. Bumper Collision
clear all
clc
% constant [s-kg/m^5]
K=30;
% mass [kg]
m = 1500;
% initial velocity [m/s]
v0=90*(1000/3600);
% initial displacement [m]
x0=0;
% time [s]
tspan = [0 1.5];
% solve DE
[t, x] = ode45(@(t,x)odefunc(t, x, K, m), tspan, [x0; v0]);
% graphing, divide figure into 2x1 grid and place the plot at position 1
subplot(2,1,1)
% plot displacement(x) vs time(t)
plot(t, x(:,1))
xlabel('Time,t [s]')
ylabel('Displacement,x [m]')
title('Displacement vs Time')
grid on
% plot at second position
subplot(2,1,2)
% plot velocity(x) vs time (t)
plot(t, x(:,2))
xlabel('Time,t [s]')
ylabel('Veloity,v [m/s]')
title('Velocity vs Time')
grid on
% plot v vs x
figure
% plot displacement(x) vs velocity
plot(x(:,1), x(:,2))
xlabel('Displacement,x [m]')
ylabel('Velocity,v [m/s]')
title('Velocity vs Displacement')
grid on
clear all
clc
% function to define the given differential equation
function dxdt = odefunc(t, x, K, m)
x1 = x(1);
x2 = x(2);
dxdt = [x2; -(K/m)*x2^3*(x1+1)^3];
end

回答(1 个)

Cris LaPierre
Cris LaPierre 2021-3-15
Any in-file functions must be at the bottom/end of your script. This doesn't mean at the end of the code that uses it. It means at the very bottom of the file.
If you want to keep your functions with problem 1, you'll need to create a separate file for each problem. If your assignment needs to be in a single file, then all your functions will have to be moved to after problem 2.
See here.

类别

Help CenterFile Exchange 中查找有关 Spline Postprocessing 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by