sym에서 double로 변환할 수 없다는 오류가 뜨는데 어떻게 해결해야 할까요?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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에서 오류가 발생한 것 같은데 잘 모르겠습니다.
채택된 답변
Dyuman Joshi
2023년 11월 13일
0 개 추천
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
WOOJIN
2023년 11월 13일
thank u bro !
Dyuman Joshi
2023년 11월 13일
You are welcome!
WOOJIN
2023년 11월 13일
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)
Dyuman Joshi
2023년 11월 13일
"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.
WOOJIN
2023년 11월 13일
No, I got the correct output. I just wondering that is there another way to pass the function not using '@'.
Dyuman Joshi
2023년 11월 13일
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
2023년 11월 13일
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)