can anyone help me ? i keep getting this error

3 views (last 30 days)
Here is the code:
num_roots=5; num_functions=6;
%initial guess for roots (from Wolfram MathWorld)
zeros_guess=[2.4,3.8,5.1,6,7.5,8.7;...
5.5,7,8.4,9.7,11,12;...
8.6 10,11.6,13,14,16;...
11.8,13,15,16,18,19;...
15,16.4,18,19.4,21,22];
%Compute first five roots of first six Bessel functions
%Put in variable bzeros with size(bzeros) = [5, 6]
integrand = @(theta,x,n) cos(x.*sin(theta)-n*theta);
J_n = @(x) integral(@(theta)integrand(theta,x,n),0,pi);
for n=0:num_functions-1
J_n = @(x) integral(@(theta)integrand(theta,x,n),0,pi);
for k=1:num_roots
bzeros(k,n+1)=fzero(J_n,bzeros_guess(k,n+1));
end
end
%print table
fprintf('k J0(x) J1(x) J2(x) J3(x) J4(x) J5(x)\n')
for k=1:num_roots
fprintf('%i',k)
for n=0:num_functions-1
fprintf('%10.4f',bzeros(k,n+1));
end
fprintf('\n');
end
this is the output that keeps popping out:
Undefined function 'bzeros_guess' for input arguments of type 'double'.
Error in solution (line 15)
bzeros(k,n+1)=fzero(J_n,bzeros_guess(k,n+1));
what does this mean and is there a way i can rewrite the white section so that it works ?

Accepted Answer

Chunru
Chunru on 26 Sep 2022
num_roots=5; num_functions=6;
%initial guess for roots (from Wolfram MathWorld)
zeros_guess=[2.4,3.8,5.1,6,7.5,8.7;...
5.5,7,8.4,9.7,11,12;...
8.6 10,11.6,13,14,16;...
11.8,13,15,16,18,19;...
15,16.4,18,19.4,21,22];
%Compute first five roots of first six Bessel functions
%Put in variable bzeros with size(bzeros) = [5, 6]
integrand = @(theta,x,n) cos(x.*sin(theta)-n*theta);
J_n = @(x) integral(@(theta)integrand(theta,x,n),0,pi);
for n=0:num_functions-1
J_n = @(x) integral(@(theta)integrand(theta,x,n),0,pi);
for k=1:num_roots
%bzeros(k,n+1)=fzero(J_n,bzeros_guess(k,n+1));
bzeros(k,n+1)=fzero(J_n, zeros_guess(k,n+1)); % initial guess at the right hand side
end
end
%print table
fprintf('k J0(x) J1(x) J2(x) J3(x) J4(x) J5(x)\n')
k J0(x) J1(x) J2(x) J3(x) J4(x) J5(x)
for k=1:num_roots
fprintf('%i',k)
for n=0:num_functions-1
fprintf('%10.4f',bzeros(k,n+1));
end
fprintf('\n');
end
1
2.4048 3.8317 5.1356 6.3802 7.5883 8.7715
2
5.5201 7.0156 8.4172 9.7610 11.0647 12.3386
3
8.6537 10.1735 11.6198 13.0152 14.3725 15.7002
4
11.7915 13.3237 14.7960 16.2235 17.6160 18.9801
5
14.9309 16.4706 17.9598 19.4094 20.8269 22.2178
  2 Comments
Mohamed
Mohamed on 26 Sep 2022
thank you so much
you are a lifesaver
Torsten
Torsten on 26 Sep 2022
The Bessel functions are predefined in MATLAB. If it's not explicitly stated in your assignment, you don't need to evaluate them using the integral representation:

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!