I do not understand why code will not run when using varargin and am receiving too many inputs error.
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Newton-Raphson Method 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!