how to add a matrix element in a fzero function

1 次查看(过去 30 天)
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

采纳的回答

dpb
dpb 2019-4-14
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...

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Debugging and Analysis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by