I do not understand why code will not run when using varargin and am receiving too many inputs error.

3 次查看(过去 30 天)
I have been working on this code and keep getting the error
Error using Test17>@(x) func(x,e,l,i,w(j))
Too many input arguments.
Error in NewtonRaphson (line 34)
xs_new = xs - func(xs, varargin{end}) / dfunc(xs, varargin{end});
Error in Test17 (line 19)
[root, ep, n] = NewtonRaphson(@(x) func(x, e, l, i, w(j)),@(x) dfunc(x, e, l, i, w(j)), xs, epf, e, l, i, w(j));
Here is the code i have written - any explanation will help
clear
clc
close all
func = @(x, e, l, i, w) (w/(120 * e * i * l)) * (-x.^5 + 2 * l^2 * x.^3 - l^4 * x);
dfunc = @(x, e, l, i, w) (w/(120 * e * i * l)) * (-5*x.^4 + 6 * l^2 * x.^2 - l^4);
e = 50000;
l = 600;
i = 30000;
w = [1000, 2000, 5000, 10000];
xs = 900;
epf = 0.01;
root = zeros(1, length(w));
ep = zeros(1, length(w));
n = zeros(1, length(w));
for j = 1:length(w)
% Determine the approximated root and n_iter for each initial guess
[root(j), ep(j), n(j)] = NewtonRaphson(@(x) func(x, e, l, i, w(j)),@(x) dfunc(x, e, l, i, w(j)), xs, epf, e, l, i, w(j));
end
% Initialize function
function[root, ep, n] = NewtonRaphson(func, dfunc, xs, epf, varargin)
n = 0;
ep = 100;
while ep > epf
xs_new = xs - func(xs, varargin{:}) / dfunc(xs, varargin{:});
n = n + 1;
if xs_new ~= 0
ep = abs((xs_new - xs) / xs_new) *100;
end
xs = xs_new;
end
root = xs_new;
end

采纳的回答

Steven Lord
Steven Lord 2023-7-19
You're calling your Newton Raphson function like:
[root(j), ep(j), n(j)] = NewtonRaphson(@(x) func(x, e, l, i, w(j)),@(x) dfunc(x, e, l, i, w(j)), xs, epf, e, l, i, w(j));
How does this function call the function you pass into it as the first and second inputs?
xs_new = xs - func(xs, varargin{:}) / dfunc(xs, varargin{:});
What does varargin contain in this call to NewtonRaphson? Looking at the definition:
function[root, ep, n] = NewtonRaphson(func, dfunc, xs, epf, varargin)
func is @(x) func(x, e, l, i, w(j)).
dfunc is @(x) dfunc(x, e, l, i, w(j)).
xs is xs.
epf is epf.
Everything else is packed into varargin. That means it contains {e, l, i, w(j)}.
So your line of code starting with xs_new is trying to call func with five inputs. The first is xs, the second through fifth are the elements of varargin. But the way you called NewtonRaphson, you told MATLAB that the functions whose handles you passed as the first and second input arguments only accept one input argument.
Either just pass the anonymous functions func and dfunc into NewtonRaphson (don't try to wrap them again in an anonymous function) or (the way I'd prefer) eliminate the trailing inputs in your call to NewtonRaphson.
[root(j), ep(j), n(j)] = NewtonRaphson(func, dfunc, xs, epf, e, l, i, w(j)); % Approach 1 or
[root(j), ep(j), n(j)] = NewtonRaphson(@(x) func(x, e, l, i, w(j)), ...
@(x) dfunc(x, e, l, i, w(j)), xs, epf); % Approach 2

更多回答(0 个)

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by