- Tutorial: MATLAB Answersで早く的確な回答を得るためのポイント https://jp.mathworks.com/matlabcentral/answers/309720
What solution if the correction is I get
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
ryohei yanagawa
le 8 Avr 2018
Commenté : ryohei yanagawa
le 10 Avr 2018
I want to get solution "integral[0,pi()/2](gcost),g=g(t)".
Please Where should I correct?
For g, I would like to input t function with user input.
1- syms t y g
2- prompt = 'g(t)';
3- g=input(prompt);
4- y=@(t,g)g.*cos(t);
5- q=integral(y,0,pi()/2)
ーーーーーーーーーーーー
error: test>@(t,g)g.*cos(t)
error: integralCalc/iterateScalarValued (line 314) fx = FUN(t);
error: integralCalc/vadapt (line 132) [q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
error: integralCalc (line 75) [q,errbnd] = vadapt(@AtoBInvTransform,interval);
error: integral (line 88) Q = integralCalc(fun,a,b,opstruct);
error: test (line 5) q=integral(y,0,pi()/2)
>>
0 commentaires
Réponse acceptée
mizuki
le 8 Avr 2018
Modifié(e) : mizuki
le 8 Avr 2018
現在の課題は y = g * cos(t) のときに ginput 関数でスカラ変数 g を入力して、yに関する積分 integral(y, 0, pi/2) を求めたい ということで間違いないでしょうか。
g は input 関数で既に数値変数として入力しているため、無名関数のハンドルにする必要がありません。以下のコードを test.m として保存されるとエラーを回避できます。
syms t y
prompt = 'g(t)';
g = input(prompt);
y = @(t) g.*cos(t);
q = integral(y,0,pi/2)
なお、この例では g は数値変数と仮定しているため、元々 syms でシンボリック変数として定義されていた g を削除しています。g はシンボリックということであれば変更が必要です。
なお、すばやく得たい回答を得る方法が以下にまとめてあります。ご参考まで。
3 commentaires
mizuki
le 10 Avr 2018
Modifié(e) : mizuki
le 10 Avr 2018
1行ずつ実行してどこがおかしいかを確認しました。 input 関数で入力を得るところまでは問題ありませんが、y の式定義の行が
>> y = @(t) g .*cos(t)
y =
値をもつ function_handle:
@(t)g.*cos(t)
のようになり、g のシンボリック式を代入した場合
>> y = @(t) (t.^2+t+1).*cos(t)
y =
値をもつ function_handle:
@(t)(t.^2+t+1).*cos(t)
と異なる結果になっています。 この行を変更する必要があります。
無名関数を使用せずに計算してから matlabFunction 関数を使用して無名関数に直して integral を解くという以下の方法ではいかがでしょうか。(式として定義されているため、y も syms から削除しています。)
clear all;
syms t
prompt = 'g(t)';
g = input(prompt);
y = g .* cos(t)
y_anony = matlabFunction(y)
q = integral(y_anony,0,pi/2)
y を無名関数として定義したい場合は、無名関数として定義した後にシンボリック式に変換する方法もあります。
clear all
syms t
prompt = 'g(t)';
g = input(prompt);
y = @(t) g .*cos(t);
y_sym = sym(y);
y_anony = matlabFunction(y_sym)
q = integral(y_anony,0,pi/2)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur MATLAB におけるシンボリック計算 dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!