how to add a matrix element in a fzero function

1 view (last 30 days)
Hi guys,
i m trying to solve x*besselj(1,x)/besselj(0,x)-J(m) using fzero function
My code is
n = 100;
J = [0.5 2 5 10 20 30 50 100];
for i = 1:n
A(i,1)=fzero('x*besselj(1,x)/besselj(0,x)-J(1)',i);
end
for i = 1:n
A(i,2)=fzero('x*besselj(1,x)/besselj(0,x)-J(2)',i);
end
for i = 1:n
A(i,3)=fzero('x*besselj(1,x)/besselj(0,x)-J(3)',i);
end
for i = 1:n
A(i,3)=fzero('x*besselj(1,x)/besselj(0,x)-J(4)',i);
end
for i = 1:n
A(i,3)=fzero('x*besselj(1,x)/besselj(0,x)-J(5)',i);
end
for i = 1:n
A(i,3)=fzero('x*besselj(1,x)/besselj(0,x)-J(6)',i);
end
for i = 1:n
A(i,3)=fzero('x*besselj(1,x)/besselj(0,x)-J(7)',i);
end
for i = 1:n
A(i,3)=fzero('x*besselj(1,x)/besselj(0,x)-J(8)',i);
end
i am trying to write the code above
n = 100;
J = [0.5 2 5 10 20 30 50 100];
for j = 1:8
for i = 1:n
A(i,m)=fzero('x*besselj(1,x)/besselj(0,x)-J(m)',i);
end
end
like that. when i run the code i get this error;
Error using fzero (line 306)
FZERO cannot continue because user-supplied expression ==> x*besselj(1,x)/besselj(0,x)-J(m) failed with the error below.
Error in inline expression ==> x*besselj(1,x)/besselj(0,x)-J(m)
Undefined function or variable 'm'.
Error in Untitled (line 6)
A(i,m)=fzero('x*besselj(1,x)/besselj(0,x)-J(m)',i);
Can you help to solve this.
Thanks

Accepted Answer

dpb
dpb on 14 Apr 2019
As you wrote it, the function is the literal text including the argument so nothing ever gets substituted for dynamically--one way that mimics yours directly but does get the current value of J() would be
...
for i = 1:n
fnA=@(x) x*besselj(1,x)/besselj(0,x)-J(i);
A(i,n)=fzero(fnA,0);
end
that embeds current J(i) into the function handle.
I just used a constant zero for the initial guess, that worked for the first couple; I didn't check if need better for the full range...
  1 Comment
dpb
dpb on 14 Apr 2019
many thanks for your answer, i solved it [-- Answer moved to comment dpb]

Sign in to comment.

More Answers (0)

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!