sym에서 double로 변환할 수 없다는 오류가 뜨는데 어떻게 해결해야 할까요?
显示 更早的评论
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에서 오류가 발생한 것 같은데 잘 모르겠습니다.
采纳的回答
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 个评论
thank u bro !
You are welcome!
how can I pass the function without using '@'? I saved the fuction golden() as golden.m and function f=trig(x) as trig.m. (it means I saved those 2 functions in each other m-file)
"how can I pass the function without using '@'? "
You can not. Why do you ask? Do you get an error?
Even if you have saved the functions separately, this syntax should work. Just make sure that the files are present in the current directory/folder.
No, I got the correct output. I just wondering that is there another way to pass the function not using '@'.
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
I fully understand, thank u !
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB Mobile 기본 사항 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)