sym에서 double로 변환할 수 없다는 오류가 뜨는데 어떻게 해결해야 할까요?

8 views (last 30 days)
WOOJIN
WOOJIN on 13 Nov 2023
Commented: WOOJIN on 13 Nov 2023
function golden(f,a,b,tolx,toly)
r=(3-sqrt(5))/2;
c=1-r;
x1=a+r*(b-a);
x2=a+c*(b-a);
f1=feval(f,x1);
f2=feval(f,x2);
k=0;
fprintf('\n')
disp(' Golden Section Search ')
fprintf('\n')
disp('_______________________________________________________')
disp('k a x1 x2 b f(x1) f(x2)')
disp('_______________________________________________________')
fprintf('\n')
while (abs(b-a)>tolx)|(abs(f2-f1)>toly)
fprintf('%2.f %10.7f %10.7f %10.7f %10.7f %10.7f %10.7f\n',k,a,x1,x2,b,f1,f2)
if (f1<f2)
b=x2;
x2=x1;
x1=a+r*(b-a);
f2=f1;
f1=feval(f,x1);
else
a=x1;
x1=x2;
x2=a+c*(b-a);
f1=f2;
f2=feval(f,x2);
end
k=k+1;
end
fprintf('\n minimum = %14.10f',f1)
fprintf('at x = %14.10f',b)
function f=trig(x)
f=cos(x)-sin(x)
golden('f',1,3,10^-8,10^-8)을 구하고자 하는데
fprintf('%2.f %10.7f %10.7f %10.7f %10.7f %10.7f %10.7f\n',k,a,x1,x2,b,f1,f2) 여기서 sym에서 double로 변환할 수 없다는 오류가 뜹니다. k,a,x1,x2,b는 출력이 되고 f1,f2가 출력되지 않는 걸 보아 f1,f2에서 오류가 발생한 것 같은데 잘 모르겠습니다.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 13 Nov 2023
You need to pass the function to be analysed as a function handle -
% vvvvv
golden(@trig,1,3,10^-8,10^-8)
Golden Section Search _______________________________________________________ k a x1 x2 b f(x1) f(x2) _______________________________________________________ 0 1.0000000 1.7639320 2.2360680 3.0000000 -1.1733444 -1.4040220 1 1.7639320 2.2360680 2.5278640 3.0000000 -1.4040220 -1.3934259 2 1.7639320 2.0557281 2.2360680 2.5278640 -1.3508548 -1.4040220 3 2.0557281 2.2360680 2.3475242 2.5278640 -1.4040220 -1.4141604 4 2.2360680 2.3475242 2.4164079 2.5278640 -1.4141604 -1.4116506 5 2.2360680 2.3049517 2.3475242 2.4164079 -1.4123572 -1.4141604 6 2.3049517 2.3475242 2.3738354 2.4164079 -1.4141604 -1.4139935 7 2.3049517 2.3312629 2.3475242 2.3738354 -1.4137741 -1.4141604 8 2.3312629 2.3475242 2.3575742 2.3738354 -1.4141604 -1.4142122 9 2.3475242 2.3575742 2.3637854 2.3738354 -1.4142122 -1.4141728 10 2.3475242 2.3537354 2.3575742 2.3637854 -1.4142093 -1.4142122 11 2.3537354 2.3575742 2.3599466 2.3637854 -1.4142122 -1.4142036 12 2.3537354 2.3561079 2.3575742 2.3599466 -1.4142136 -1.4142122 13 2.3537354 2.3552017 2.3561079 2.3575742 -1.4142129 -1.4142136 14 2.3552017 2.3561079 2.3566679 2.3575742 -1.4142136 -1.4142134 15 2.3552017 2.3557617 2.3561079 2.3566679 -1.4142134 -1.4142136 16 2.3557617 2.3561079 2.3563218 2.3566679 -1.4142136 -1.4142136 17 2.3557617 2.3559757 2.3561079 2.3563218 -1.4142135 -1.4142136 18 2.3559757 2.3561079 2.3561896 2.3563218 -1.4142136 -1.4142136 19 2.3561079 2.3561896 2.3562401 2.3563218 -1.4142136 -1.4142136 20 2.3561079 2.3561584 2.3561896 2.3562401 -1.4142136 -1.4142136 21 2.3561584 2.3561896 2.3562089 2.3562401 -1.4142136 -1.4142136 22 2.3561584 2.3561777 2.3561896 2.3562089 -1.4142136 -1.4142136 23 2.3561777 2.3561896 2.3561970 2.3562089 -1.4142136 -1.4142136 24 2.3561896 2.3561970 2.3562015 2.3562089 -1.4142136 -1.4142136 25 2.3561896 2.3561941 2.3561970 2.3562015 -1.4142136 -1.4142136 26 2.3561896 2.3561924 2.3561941 2.3561970 -1.4142136 -1.4142136 27 2.3561924 2.3561941 2.3561952 2.3561970 -1.4142136 -1.4142136 28 2.3561924 2.3561935 2.3561941 2.3561952 -1.4142136 -1.4142136 29 2.3561935 2.3561941 2.3561946 2.3561952 -1.4142136 -1.4142136 30 2.3561941 2.3561946 2.3561948 2.3561952 -1.4142136 -1.4142136 31 2.3561941 2.3561944 2.3561946 2.3561948 -1.4142136 -1.4142136 32 2.3561944 2.3561946 2.3561947 2.3561948 -1.4142136 -1.4142136 33 2.3561944 2.3561945 2.3561946 2.3561947 -1.4142136 -1.4142136 34 2.3561944 2.3561945 2.3561945 2.3561946 -1.4142136 -1.4142136 35 2.3561945 2.3561945 2.3561945 2.3561946 -1.4142136 -1.4142136 36 2.3561945 2.3561945 2.3561945 2.3561945 -1.4142136 -1.4142136 37 2.3561945 2.3561945 2.3561945 2.3561945 -1.4142136 -1.4142136 38 2.3561945 2.3561945 2.3561945 2.3561945 -1.4142136 -1.4142136 39 2.3561945 2.3561945 2.3561945 2.3561945 -1.4142136 -1.4142136 minimum = -1.4142135624 at x = 2.3561945147
function golden(f,a,b,tolx,toly)
r=(3-sqrt(5))/2;
c=1-r;
x1=a+r*(b-a);
x2=a+c*(b-a);
f1=feval(f,x1);
f2=feval(f,x2);
k=0;
fprintf('\n')
disp(' Golden Section Search ')
fprintf('\n')
disp('_______________________________________________________')
disp('k a x1 x2 b f(x1) f(x2)')
disp('_______________________________________________________')
fprintf('\n')
while (abs(b-a)>tolx)|(abs(f2-f1)>toly)
fprintf('%2.f %10.7f %10.7f %10.7f %10.7f %10.7f %10.7f\n',k,a,x1,x2,b,f1,f2)
if (f1<f2)
b=x2;
x2=x1;
x1=a+r*(b-a);
f2=f1;
f1=feval(f,x1);
else
a=x1;
x1=x2;
x2=a+c*(b-a);
f1=f2;
f2=feval(f,x2);
end
k=k+1;
end
fprintf('\n minimum = %14.10f',f1)
fprintf('\nat x = %14.10f',b)
end
function f=trig(x)
f=cos(x)-sin(x);
end
  7 Comments
Dyuman Joshi
Dyuman Joshi on 13 Nov 2023
You can define it as an anonymous function, but the functionality behind it is the same.
So, No, there is no other way.
%Anonymous function
f=@(x) cos(x)-sin(x);
%pass it as an input
golden(f,1,3,10^-8,10^-8)
Golden Section Search _______________________________________________________ k a x1 x2 b f(x1) f(x2) _______________________________________________________ 0 1.0000000 1.7639320 2.2360680 3.0000000 -1.1733444 -1.4040220 1 1.7639320 2.2360680 2.5278640 3.0000000 -1.4040220 -1.3934259 2 1.7639320 2.0557281 2.2360680 2.5278640 -1.3508548 -1.4040220 3 2.0557281 2.2360680 2.3475242 2.5278640 -1.4040220 -1.4141604 4 2.2360680 2.3475242 2.4164079 2.5278640 -1.4141604 -1.4116506 5 2.2360680 2.3049517 2.3475242 2.4164079 -1.4123572 -1.4141604 6 2.3049517 2.3475242 2.3738354 2.4164079 -1.4141604 -1.4139935 7 2.3049517 2.3312629 2.3475242 2.3738354 -1.4137741 -1.4141604 8 2.3312629 2.3475242 2.3575742 2.3738354 -1.4141604 -1.4142122 9 2.3475242 2.3575742 2.3637854 2.3738354 -1.4142122 -1.4141728 10 2.3475242 2.3537354 2.3575742 2.3637854 -1.4142093 -1.4142122 11 2.3537354 2.3575742 2.3599466 2.3637854 -1.4142122 -1.4142036 12 2.3537354 2.3561079 2.3575742 2.3599466 -1.4142136 -1.4142122 13 2.3537354 2.3552017 2.3561079 2.3575742 -1.4142129 -1.4142136 14 2.3552017 2.3561079 2.3566679 2.3575742 -1.4142136 -1.4142134 15 2.3552017 2.3557617 2.3561079 2.3566679 -1.4142134 -1.4142136 16 2.3557617 2.3561079 2.3563218 2.3566679 -1.4142136 -1.4142136 17 2.3557617 2.3559757 2.3561079 2.3563218 -1.4142135 -1.4142136 18 2.3559757 2.3561079 2.3561896 2.3563218 -1.4142136 -1.4142136 19 2.3561079 2.3561896 2.3562401 2.3563218 -1.4142136 -1.4142136 20 2.3561079 2.3561584 2.3561896 2.3562401 -1.4142136 -1.4142136 21 2.3561584 2.3561896 2.3562089 2.3562401 -1.4142136 -1.4142136 22 2.3561584 2.3561777 2.3561896 2.3562089 -1.4142136 -1.4142136 23 2.3561777 2.3561896 2.3561970 2.3562089 -1.4142136 -1.4142136 24 2.3561896 2.3561970 2.3562015 2.3562089 -1.4142136 -1.4142136 25 2.3561896 2.3561941 2.3561970 2.3562015 -1.4142136 -1.4142136 26 2.3561896 2.3561924 2.3561941 2.3561970 -1.4142136 -1.4142136 27 2.3561924 2.3561941 2.3561952 2.3561970 -1.4142136 -1.4142136 28 2.3561924 2.3561935 2.3561941 2.3561952 -1.4142136 -1.4142136 29 2.3561935 2.3561941 2.3561946 2.3561952 -1.4142136 -1.4142136 30 2.3561941 2.3561946 2.3561948 2.3561952 -1.4142136 -1.4142136 31 2.3561941 2.3561944 2.3561946 2.3561948 -1.4142136 -1.4142136 32 2.3561944 2.3561946 2.3561947 2.3561948 -1.4142136 -1.4142136 33 2.3561944 2.3561945 2.3561946 2.3561947 -1.4142136 -1.4142136 34 2.3561944 2.3561945 2.3561945 2.3561946 -1.4142136 -1.4142136 35 2.3561945 2.3561945 2.3561945 2.3561946 -1.4142136 -1.4142136 36 2.3561945 2.3561945 2.3561945 2.3561945 -1.4142136 -1.4142136 37 2.3561945 2.3561945 2.3561945 2.3561945 -1.4142136 -1.4142136 38 2.3561945 2.3561945 2.3561945 2.3561945 -1.4142136 -1.4142136 39 2.3561945 2.3561945 2.3561945 2.3561945 -1.4142136 -1.4142136 minimum = -1.4142135624 at x = 2.3561945147
function golden(f,a,b,tolx,toly)
r=(3-sqrt(5))/2;
c=1-r;
x1=a+r*(b-a);
x2=a+c*(b-a);
f1=feval(f,x1);
f2=feval(f,x2);
k=0;
fprintf('\n')
disp(' Golden Section Search ')
fprintf('\n')
disp('_______________________________________________________')
disp('k a x1 x2 b f(x1) f(x2)')
disp('_______________________________________________________')
fprintf('\n')
while (abs(b-a)>tolx)|(abs(f2-f1)>toly)
fprintf('%2.f %10.7f %10.7f %10.7f %10.7f %10.7f %10.7f\n',k,a,x1,x2,b,f1,f2)
if (f1<f2)
b=x2;
x2=x1;
x1=a+r*(b-a);
f2=f1;
f1=feval(f,x1);
else
a=x1;
x1=x2;
x2=a+c*(b-a);
f1=f2;
f2=feval(f,x2);
end
k=k+1;
end
fprintf('\n minimum = %14.10f',f1)
fprintf('\nat x = %14.10f',b)
end
WOOJIN
WOOJIN on 13 Nov 2023
I fully understand, thank u !

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!