Find input to function 1 that results in function 2 returning 0
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a function that traces raypaths given input criteria. I then have a second function that solves for error at a given point in space. I am able to find the launch angles that would result in bracketing the target.
My question is this: having solved for launch angles that bracket the point, how do I find the launch angle that results in zero error? An excerpt of the code I am working with is below
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1715472/image.png)
rayangle = (-90:.1:90)
nray=length(rayangle)
for ii=1:nray
theta0=rayangle(ii)
z_r=raysolver(z0,theta0,cmat,nstep,rstep); %calling the function that traces the rays
z_r_theta0(ii,:)=z_r(1,:); %Saving the path of each ray in z_r_theta0
end
%% Finding error
%target co-ordinates
r=50000;
z=1100;
d_error=deptherror(r,z,z_r_theta0); %gives me depth error for each launch angle
bracket_ray=crossray(d_error)%this gives me all the launch angles that bracket the target.
%what I need is to find theta0 for each set of bracketing launch angles
%that result in d_error == 0
0 个评论
采纳的回答
Nipun
2024-6-14
Hi Matthew,
I understand that you want to find the launch angle that results in zero error after bracketing the target with launch angles. You can use the fzero function to achieve this.
Here’s how you can modify your script:
rayangle = (-90:.1:90);
nray = length(rayangle);
z_r_theta0 = zeros(nray, 100); % Adjust 100 to match the length of your z_r
for ii = 1:nray
theta0 = rayangle(ii);
z_r = raysolver(z0, theta0, cmat, nstep, rstep); % Calling the function that traces the rays
z_r_theta0(ii,:) = z_r(1,:); % Saving the path of each ray in z_r_theta0
end
% Target coordinates
r = 50000;
z = 1100;
d_error = deptherror(r, z, z_r_theta0); % Gives me depth error for each launch angle
bracket_ray = crossray(d_error); % This gives me all the launch angles that bracket the target.
% Find the launch angle for zero error
theta0_zero_error = fzero(@(theta) deptherror(r, z, raysolver(z0, theta, cmat, nstep, rstep)), [min(bracket_ray), max(bracket_ray)]);
disp(['Launch angle with zero error: ', num2str(theta0_zero_error)]);
Make sure that deptherror and raysolver are defined correctly, and the brackets in fzero cover the range of angles that bracket the target.
Refer to the following MathWorks documentation for "fzero" function in MATLAB: https://www.mathworks.com/help/matlab/ref/fzero.html
Hope this helps.
Nipun
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!