FZero NaN help!
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Iv got this question: Consider the curve defined by y = 40*cos(0.1x + 0.2). The distance d(x) from the origin (0, 0) to a point (x, y) on the curve is
d(x) = sqrt(x^2 + (40*cos(0.1x + 0.2))^2)
(a) Write a MATLAB program and function M-file to plot d(x) for −100 ≤ x ≤ 100. Also use the function M-file and the fminbnd command to find (and display) the point (xmin,ymin) on the curve that is closest to the origin and display the minimum distance (i.e., the distance from the origin to the point (xmin,ymin)).
(b) Write a MATLAB program that uses the fzero command to find the point(s) on the curve at a distance of 60 from the origin.
i have a function file:
function D = q4(x)
D = sqrt(x.^2 + ((40*cos((0.1*x) + 0.2)).^2));
part a is this:
clear
format short
x = (-100:0.0001:100);
D=q4(x); %Sending x values to function
plot(x,D)
xlabel('d(x)')
ylabel('Y')
grid
pause
x1=input('Enter left hand endpoint of subinterval: ');
x2=input('Enter right hand endpoint of subinterval: ');
xmin=fminbnd('q4', x1,x2);%Finding xmin
ymin=q4(xmin);%finding ymin
hold on;
plot(xmin,ymin,'rO')%showing point on plot
dis=xmin;
%display functions
disp('xmin');
disp(xmin);
disp('ymin');
disp(ymin);
disp('Distance from the origin: ');
disp(dis);
the only issue i have is part B where using fzero, as the function doesnt cut the x axis i didnt think you could run it. My lecturer said it needs to be when d(x)=60 but im unsure of how to go about doing that. Also d(x)-60 =0 is also taken into account.
1 comentario
Aaron Brown
el 5 de Abr. de 2017
Hi, did you end up getting this sorted. I have run into a similar issue.
Respuestas (1)
Star Strider
el 4 de Abr. de 2017
Solving for ‘d(x)-60’ is the correct approach.
Once you have written your anonymous function to define ‘d(x)’, you need to write a second one (it can be a direct argument to the fzero function) to create the 60 offset:
x0 = initial guess;
x60 = fzero(@(x) d(x)-60, x0)
2 comentarios
Ver también
Categorías
Más información sobre Interpolation of 2-D Selections in 3-D Grids en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!