Undefined function or variable

3 views (last 30 days)
Brian Meermans
Brian Meermans on 29 Sep 2017
Answered: Walter Roberson on 29 Sep 2017
Hello im stuck and keep getting the Error:
Undefined function or variable 'newtraphlab5'.
Error in Lab5hw (line 18)
[root,iter,ea] = newtraphlab5(func,dfunc,Vo); % obtain the
result
here is my Lab5hw.m
% The van der waals equation relates the pressure, volume and temperature
% use Newton Raphson method to find the root
clc;clear; % Clearing the Command Window and Workspace
% Setting the parameters
n = 1; % number of moles
p = 100.00; % Pressure
T = 300.0; % temperature
a = 1.360; % The unknown constant
b = 0.003183; % The unknown constant
R = 0.0820578; % The gas constant
Vo = 0.246; % Initial guess
% The function to find the root
func = @(V) (p+(n.^2*a)/(V.^2)).*(V-n*b)-n*R*T; % The Ven der Waals equation
% The derivative of the function to find the root
dfunc = @(V) (a*n.^2*(2*b*n-V))./(V.^3)+p; % The derivative of Ven der Waals equation
% Call the function to get the root
[root,iter,ea] = newtraphlab5(func,dfunc,Vo); % obtain the result
% Display the results
fprintf('Volume of one mole of Oxygen = %f\n',root);
fprintf('Number of iterations taken to get the resut = %d\n',iter);
fprintf('Percentage Error = %e\n',ea);
here is my newtraphlab5.m*
function [root,iter,ea] = newtraph(func,dfunc,xr,es,maxit)
% Inputs:
%
%
%
% Outputs:
%
if nargin<5 % if number of arguments is less then 5 then max is set to 50
maxit=50; % sets the max interactions =50
end
if nargin<4 % if number of arguments less then 4 then st step size .01
es=0.01; % step size .01
end
iter = 0; % iteration set to 0
while (1) % while loop for 1
xrold = xr; % sets the old gas to xr so we can change it later on and run through multiple iterations
xr = xr - func(xr)/dfunc(xr); % this is the formula for the newtraph
iter = iter + 1; % takes our iteration and adds one to the original to use as the next gues point
if xr~=0 % if then
ea = 100*abs((xr-xrold)/xr); % fiding the error
xrv(iter) = xr; %root vector storing to grab values later
eav(iter) = ea; %error vector storing to grab values later
inerv(iter) = iter; %iteration vector storing to grab values later
end
if ea <= es || iter >= maxit % if error is greater or equal if true then set interations lessthen or equal to maxit. if not then stop
break % stopes statements from being executed after this point
end
end
root = double(xr);
subplot(2,1,1) % subplot makes it able to print multiple graphs on 1 window has 2 rows 1 column and 1 Position
plot(inerv,xrv)% plots with x,y axis
xlabel('iteration'); %label x
ylabel('root estimate'); %label y
subplot(2,1,2)% subplot makes it able to print multiple graphs on 1 window has 2 rows 1 column and 2 Position
plot(inerv,eav)% plots with x,y axis
xlabel('iteration'); %label x
ylabel('Error'); %labely
end

Answers (1)

Walter Roberson
Walter Roberson on 29 Sep 2017
You have
[root,iter,ea] = newtraphlab5(func,dfunc,Vo); % obtain the result
which refers to a function named newtraphlab5 .
You have
function [root,iter,ea] = newtraph(func,dfunc,xr,es,maxit)
which defines a function named newtraph .
It is an oddity of MATLAB that if the above function were the first function in a file named newtraphlab5.m then you would call it with the file name rather than the function name it is defined as being. However, if that is happening, chances are high that you have made a code mistake or a mistake in your MATLAB path.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!