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
댓글 수: 0
채택된 답변
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...
댓글 수: 1
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!