Clear Filters
Clear Filters

How to properly call a function from another matlab-file?

2 views (last 30 days)
I have this code for Newton's method :
% Newton's Method
% Computes approximate solution using Newton's method
% with the help of a tolerance/error check
% Input: function handle f, the derivative of the function df,
% starting guess x0, tolerance for error, and nr of iteration steps k
% Output: Approximate solution xc
function x = mynewton(f, fd, x0, tol)
format longg;
x1 = x0; % Set starting value
y = f(x1); % The absolute value of y = absolute error
% max_iter = k; % Set max nr of iterations
n = 0; % Initialize n (counts iterations)
%error = 1; % Initialize error
values = zeros(x1, length(tol)); % Initialize array of iterations
i = 1; % Initialize i
while abs(y) > tol % Repeats this step until tolerance is reached
x = x1 - y/fd(x1); % Newton's method (add semi-colons and see)
values(i) = x; % Add next iteration to arrat
%error = abs(x-x1); % Compute error
x1 = x; % Update x
y = f(x);
n = n + 1; % Update n
i = i+1; % Update i
end
disp(values);
iter_nr = size(values, 1) - 1;
display("Number of iterations: " + iter_nr);
end
When I run it, I get this output:
>> mynewton(g, gd, 2, 0.5*(10^(-10)))
1.85196350463693
1.83079530344633
1.83029075075994
1.83029046062238
"Number of iterations: 3"
ans =
1.83029046062238
In the code below, I am trying to call my function from another MATLAB-file:
% NON-LINEAR FUNCTIONS
% Computes approximate solution of f(x) = 0 using Newton's method, Euler's
% Backwards method, and the secant method
% Input: function f, its derivative fd, a, b, s.t. f(a) * f(b) < 0, and
% initial guesses x0, x1
% Output: Approximate solution xc
function non_lin(a, b, x0, x1, n)
format longg;
f = @(x) (x^3)+(10*cos(2*x))+(log(x+11));
fd = @(x)(3*x^2)+(1/(x+11))-20*sin(2*x);
tolerance = 0.5*(10^(-10));
disp("Newton's method:");
mynewton(f, fd, x0, tolerance);
disp('\n');
disp("Secant method:");
mysecant(f, x0, x1, tolerance, n);
disp('\n');
disp("Bisect method:");
mybisect(f, a, b, tolerance);
disp('\n');
end
But I get this error:
>> non_lin(-1, 0, 1.5, 2, 20)
Newton's method:
Error using zeros
Size inputs must be integers.
Error in mynewton (line 17)
values = zeros(x1, length(tol)); % Initialize array of iterations
Error in non_lin (line 17)
mynewton(f, fd, x0, tolerance);
I don't understand why. I have defined the tolerance to be the same as the one I give as argument to mynewton(). What am I doing wrong here?
  1 Comment
Rik
Rik on 6 Oct 2022
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.

Sign in to comment.

Accepted Answer

Jan
Jan on 5 Oct 2022
zeros(x1, length(tol)) creates a matrix of zeros with x1 rows and length(tol) columns. If x1 is an integer value, this works. But you cannot assume the initial value to be a matching integer. Maybe you mean:
zeros(numel(x1), numel(tol))
numel() is safer than length(), which chooses the longest dimension.
But length(tol) is very strange already: The tolerance is a scalar usually. In the help section "nr of iteration steps k" is mentioned, but there is no nr.
What is the purpose to increase i and n in each iteration? These counters are redundant. After the loop the number of iterations is determined by the size of values, but you can use i or n directly instead.
So the problem is not how to call the function from another function or script, but the function for the Newton iteration is buggy. Clean up this function.
By the way, compare 0.5*(10^(-10)) with 0.5e-10. The first one is harder to read and contains an expensive power operation, while the sendond one is a cheap constant.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!