MATLAB Answers

0

how to add a matrix element in a fzero function

Asked by ekrem yilmaz on 14 Apr 2019
Latest activity Commented on by dpb
on 14 Apr 2019
Accepted Answer by dpb
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

  0 Comments

Sign in to comment.

Tags

1 Answers

Answer by dpb
on 14 Apr 2019
 Accepted Answer

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 Comments

many thanks for your answer, i solved it [-- Answer moved to comment dpb]

Sign in to comment.



Translated by