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 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)