FZero NaN help!

4 ビュー (過去 30 日間)
Bryce Standkey
Bryce Standkey 2017 年 4 月 4 日
コメント済み: Aaron Brown 2017 年 4 月 5 日
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 件のコメント
Aaron Brown
Aaron Brown 2017 年 4 月 5 日
Hi, did you end up getting this sorted. I have run into a similar issue.

サインインしてコメントする。

回答 (1 件)

Star Strider
Star Strider 2017 年 4 月 4 日
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 件のコメント
Bryce Standkey
Bryce Standkey 2017 年 4 月 4 日
clear
format short
x = (-100:1:100);
D=q4(x); %Sending x values to function
plot(x,D)
xlabel('d(x)')
ylabel('Y')
grid
x0 = 55;
x60 = fzero(@(x) D-60, x0);
returns error: >> q4b Error using fzero (line 167) FUN must be a function, a valid string expression, or an inline function object.
Error in q4b (line 13) x60 = fzero(D-60, x0);
Star Strider
Star Strider 2017 年 4 月 4 日

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInterpolation of 2-D Selections in 3-D Grids についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by