Solving an equation in Matlab
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Dear, I'm trying to resolve this equation for my thesis project, u and v are the values that I try to calculate, the only parameter that change is the Tx and Ty, all the other parameters are constant. I appreciate any help

tx = [0.1 0.5 0.12 0.14 0.22 0.1 0.12 0.29]
ty = [0.4 0.12 0.34 0.14 0.13 0.03 0.2 0.13]
Omega = 7.3E-5;
latitude = -12;
f0 = 2*Omega*sind(latitude);
r = 1./(2.*24.*3600)
Hm = 25
rho = 1.025e3
dvdt0 = -r.*v0 - f0.*u0 -ty./(rho.*Hm);
dudt0 = -r.*u0 + f0.*v0 + tx./(rho.*Hm);
採用された回答
Star Strider
2020 年 2 月 12 日
These have analytic solutions:
syms u(t) v(t) f taux tauy Hm rho R u0 v0
du = diff(u);
dv = diff(v);
DE = [du - f*v == taux/(Hm*rho) - R*u; dv - f*u == taux/(Hm*rho) - R*v]
Soln = dsolve(DE, u(0)==u0, v(0)==v0)
u_Soln = Soln.u;
v_Soln = Soln.v;
u_Soln = simplify(u_Soln, 'Steps',500)
v_Soln = simplify(v_Soln, 'Steps',500)
u_fcn = matlabFunction(u_Soln)
v_fcn = matlabFunction(v_Soln)
Note that ‘u_fcn’ and ‘v_fcn’ are anonymous functions you can use outside of the Symbolic Math Toolbox.
10 件のコメント
Pablo Estuardo
2020 年 2 月 12 日
Dear Star Stride, thanks you so much for your help and time, I'm trying to understand the error in the line Soln = dsolve(DE, u(0)==u0, v(0)==v0), I will apreciate any help.
Error using mupadengine/feval (line 163)
The equations are invalid.
Error in dsolve>mupadDsolve (line 336)
T = feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 193)
sol = mupadDsolve(args, options);
clear all
close all
taux = [0.1 0.5 0.12 0.14 0.22 0.1 0.12 0.29]
tauy = [0.4 0.12 0.34 0.14 0.13 0.03 0.2 0.13]
Omega = 7.3E-5;
latitude = -12;
f = 2*Omega*sind(latitude);
R = 1./(2.*24.*3600)
Hm = 25
rho = 1.025e3
%----------------------------
syms u(t) v(t) f taux tauy Hm rho R u0 v0
du = diff(u);
dv = diff(v);
DE = [du - f*v == taux/(Hm*rho) - R*u; dv + f*u == tauy/(Hm*rho) - R*v]
% dvdt0 = -r.*v0 - f.*u0 -taux./(rho.*H);
% dudt0 = -r.*u0 + f.*v0 +tauy./(rho.*H);
% initial conditions for analytic solution
% u0 = 0;
% v0 = 0;
Soln = dsolve(DE, u(0)==u0, v(0)==v0)
Error using mupadengine/feval (line 163)
The equations are invalid.
Error in dsolve>mupadDsolve (line 336)
T = feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 193)
sol = mupadDsolve(args, options);
u_Soln = Soln.u;
v_Soln = Soln.v;
u_Soln = simplify(u_Soln, 'Steps',500)
v_Soln = simplify(v_Soln, 'Steps',500)
u_fcn = matlabFunction(u_Soln)
v_fcn = matlabFunction(v_Soln)
Star Strider
2020 年 2 月 12 日
I have no problems with it in R2019b.
This code:
syms u(t) v(t) f taux tauy Hm rho R u0 v0
du = diff(u);
dv = diff(v);
DE = [du - f*v == taux/(Hm*rho) - R*u; dv - f*u == taux/(Hm*rho) - R*v]
Soln = dsolve(DE, u(0)==u0, v(0)==v0)
u_Soln = Soln.u;
v_Soln = Soln.v;
u_Soln = simplify(u_Soln, 'Steps',500)
v_Soln = simplify(v_Soln, 'Steps',500)
u_fcn = matlabFunction(u_Soln)
v_fcn = matlabFunction(v_Soln)
produces these results:
u_Soln =
exp(-t*(R + f))*(u0/2 - v0/2) - (exp(-t*(R - f))*(2*taux - 2*taux*exp(t*(R - f)) - Hm*R*rho*u0 - Hm*R*rho*v0 + Hm*f*rho*u0 + Hm*f*rho*v0))/(2*Hm*rho*(R - f))
v_Soln =
- exp(-t*(R + f))*(u0/2 - v0/2) - (exp(-t*(R - f))*(2*taux - 2*taux*exp(t*(R - f)) - Hm*R*rho*u0 - Hm*R*rho*v0 + Hm*f*rho*u0 + Hm*f*rho*v0))/(2*Hm*rho*(R - f))
u_fcn = @(Hm,R,f,rho,t,taux,u0,v0)exp(-t.*(R+f)).*(u0./2.0-v0./2.0)-(exp(-t.*(R-f)).*(taux.*2.0-taux.*exp(t.*(R-f)).*2.0-Hm.*R.*rho.*u0-Hm.*R.*rho.*v0+Hm.*f.*rho.*u0+Hm.*f.*rho.*v0))./(Hm.*rho.*(R-f).*2.0)
v_fcn = @(Hm,R,f,rho,t,taux,u0,v0)-exp(-t.*(R+f)).*(u0./2.0-v0./2.0)-(exp(-t.*(R-f)).*(taux.*2.0-taux.*exp(t.*(R-f)).*2.0-Hm.*R.*rho.*u0-Hm.*R.*rho.*v0+Hm.*f.*rho.*u0+Hm.*f.*rho.*v0))./(Hm.*rho.*(R-f).*2.0)
I edited the output slightly to make it readable.
Pablo Estuardo
2020 年 2 月 13 日
編集済み: Pablo Estuardo
2020 年 2 月 13 日
Dear Star Strider, thank for your time. I run the code, but the initial conditions added in the dsolve code it seems to not work in my version of matlab 2016a (without the initial conditions works), i found that I can add the initial conditions like:
cond = [u(0) == u0,v(0)==v0];
sol = dsolve(DE,cond)
but also did not work..
>> Soln = dsolve(DE, u(0)==0, v(0)==0)
Error using mupadengine/feval (line 163)
The equations are invalid.
Error in dsolve>mupadDsolve (line 336)
T = feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 193)
sol = mupadDsolve(args, options);
>> Soln = dsolve(DE)
Soln =
v: [1x1 sym]
u: [1x1 sym]
>> Soln.u
ans =
exp(-t*(R - f))*(C2 + (taux*exp(R*t - f*t))/(Hm*rho*(R - f))) - C1*exp(-t*(R + f))
Star Strider
2020 年 2 月 13 日
Do not use the ‘taux’ and ‘tauy’ data (or any of the others) when calculating the solutions to the differential equations. Leave them until the functions are evaluated.
Try this (comment-out the symbolic derivations and resulting functions first):
u_fcn = @(Hm,R,f,rho,t,taux,tauy,u0,v0)exp(-t.*(R-f)).*(u0./2.0+v0./2.0-(taux+tauy)./(Hm.*rho.*(R-f).*2.0)+(exp(t.*(R-f)).*(taux+tauy))./(Hm.*rho.*(R-f).*2.0))+exp(-t.*(R+f)).*(u0./2.0-v0./2.0-(taux-tauy)./(Hm.*rho.*(R+f).*2.0)+(exp(t.*(R+f)).*(taux-tauy))./(Hm.*rho.*(R+f).*2.0));
v_fcn = @(Hm,R,f,rho,t,taux,tauy,u0,v0)exp(-t.*(R-f)).*(u0./2.0+v0./2.0-(taux+tauy)./(Hm.*rho.*(R-f).*2.0)+(exp(R.*t-f.*t).*(taux+tauy))./(Hm.*rho.*(R-f).*2.0))-exp(-t.*(R+f)).*(u0./2.0-v0./2.0-(taux-tauy)./(Hm.*rho.*(R+f).*2.0)+(exp(R.*t+f.*t).*(taux-tauy))./(Hm.*rho.*(R+f).*2.0));
taux = [0.1 0.5 0.12 0.14 0.22 0.1 0.12 0.29]
tauy = [0.4 0.12 0.34 0.14 0.13 0.03 0.2 0.13]
Omega = 7.3E-5;
latitude = -12;
f = 2*Omega*sind(latitude);
R = 1./(2.*24.*3600);
Hm = 25;
rho = 1.025e3;
u0 = 0; % Substitute Correct Initial Condition
v0 = 0; % Substitute Correct Initial Condition
[Taux,Tauy] = ndgrid(taux, tauy); % Create Matrices From The Vectors
tv = linspace(0, 100, 3);
u_fcnt = @(t) u_fcn(Hm,R,f,rho,t,Taux,Tauy,u0,v0); % Function Only Of ‘t’ Here
v_fcnt = @(t) v_fcn(Hm,R,f,rho,t,Taux,Tauy,u0,v0); % Function Only Of ‘t’ Here
u = u_fcnt(1);
v = v_fcnt(1);
figure
hold on
for k = 1:numel(tv)
surf(taux, tauy, u_fcnt(tv(k)))
end
hold off
grid on
view(30, 30)
xlabel('\tau_x')
ylabel('\tau_y')
zlabel('u')
figure
hold on
for k = 1:numel(tv)
surf(taux, tauy, v_fcnt(tv(k)))
end
hold off
grid on
view(30, 30)
xlabel('\tau_x')
ylabel('\tau_y')
zlabel('\nu')
Also, there was an error. The correct expression for ‘DE’ is:
DE = [du - f*v == taux/(Hm*rho) - R*u; dv - f*u == tauy/(Hm*rho) - R*v]
I already corrected for that in the code for ‘u_fcn’ and ‘v_fcn’ and the rest in the code in this Comment.
Note: The plots appear to be a bit strange, because ‘taux’ and ‘tauy’ have repeated elements, and they are not sorted. You may not want (or need) to plot them, hoiwever this code demonstrates the correct way to evaluate them. They are functions of three variables
, so that must be accounted for in evaluating them.
Pablo Estuardo
2020 年 2 月 13 日
Thanks Star, I notice about the error, the other error is the sing dv + f*u (can you please add the u_fcn and v_fcn solutions), I try to fixing but i can get the dsolve works, I definitely going to update Matlab to 2019, I really apreciate all your help (I'm learning a lot in the process). At the end my porpose is to compare the vector u and v, of this model with real insitu data.

% DE = [du - f*v == taux/(Hm*rho) - R*u; dv + f*u == tauy/(Hm*rho) - R*v]
clear all
close all
syms u(t) v(t) f taux tauy Hm rho R u0 v0
du = diff(u);
dv = diff(v);
DE = [du - f*v == taux/(Hm*rho) - R*u; dv + f*u == tauy/(Hm*rho) - R*v]
Soln = dsolve(DE, u(0)==u0, v(0)==v0)
Error using mupadengine/feval (line 163)
The equations are invalid.
Error in dsolve>mupadDsolve (line 336)
T = feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 193)
sol = mupadDsolve(args, options);
u_Soln = Soln.u;
v_Soln = Soln.v;
u_Soln = simplify(u_Soln, 'Steps',500)
v_Soln = simplify(v_Soln, 'Steps',500)
u_fcn = matlabFunction(u_Soln)
v_fcn = matlabFunction(v_Soln)
Star Strider
2020 年 2 月 13 日
What does ‘... the other error is the sing dv + f*u ...’ mean? I coded the equations as written.
Pablo Estuardo
2020 年 2 月 13 日
編集済み: Pablo Estuardo
2020 年 2 月 13 日

DE = [du - f*v == taux/(Hm*rho) - R*u; dv "+" f*u == tauy/(Hm*rho) - R*v] right?
------
% your comment
Also, there was an error. The correct expression for ‘DE’ is:
DE = [du - f*v == taux/(Hm*rho) - R*u; dv "-" f*u == tauy/(Hm*rho) - R*v]
Star Strider
2020 年 2 月 13 日
That is what I coded, yes.
Pablo Estuardo
2020 年 2 月 13 日
編集済み: Pablo Estuardo
2020 年 2 月 13 日
Thanks Star, you helping me a lot, now im going to try to make the dsolve works, in that way I can add more terms to try new solutions, again, thanks you so much for your time... best regards.
syms u(t) v(t) f taux tauy Hm rho R u0 v0
du = diff(u);
dv = diff(v);
DE = [du - f*v == taux/(Hm*rho) - R*u; dv + f*u == tauy/(Hm*rho) - R*v]
Soln = dsolve(DE, u(0)==u0, v(0)==v0)
u_Soln = Soln.u;
v_Soln = Soln.v;
u_Soln = simplify(u_Soln, 'Steps',500)
v_Soln = simplify(v_Soln, 'Steps',500)
u_fcn = matlabFunction(u_Soln)
v_fcn = matlabFunction(v_Soln)
Star Strider
2020 年 2 月 13 日
As always, my pleasure!
I will help as I can.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Structural Mechanics についてさらに検索
製品
タグ
参考
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)
