Solving non-linear equation in vector form

12 ビュー (過去 30 日間)
Tayyab Khalil
Tayyab Khalil 2021 年 4 月 28 日
コメント済み: Tayyab Khalil 2021 年 4 月 28 日
Hi all, hope you are doing well.
Soi have a simple equation where the known value is a vector. So i need to get a vector as the solution.
The equation is very simple and can be easily caluclated by hand but i require it to be solved using Matlab.
Here is the code i have tried:
u2 = rand(1,1000);
syms t1
eq = t1.^2/64 == u2;
solve(eq, t1)
Any help would be appreciated, thanks.

採用された回答

Matt J
Matt J 2021 年 4 月 28 日
編集済み: Matt J 2021 年 4 月 28 日
If you have the Optimization Toolbox,
u2=[1,4,9];
opts=optimoptions('fsolve','SpecifyObjectiveGradient',true,'OptimalityTolerance',1e-12);
t1=fsolve(@(t1)objfunc(t1,u2),u2,opts)
Equation solved, inaccuracy possible. The vector of function values is near zero, as measured by the value of the function tolerance. However, the last step was ineffective.
t1 = 1×3
8.0000 16.0000 24.0000
function [err, J]=objfunc(t1,u2)
err=t1.^2/64-u2;
J=speye(numel(u2))/32; %Jacobian
end
  1 件のコメント
Tayyab Khalil
Tayyab Khalil 2021 年 4 月 28 日
Yes that works fine, but is there not a easier way to solve this relatively simple problem?

サインインしてコメントする。

その他の回答 (2 件)

Matt J
Matt J 2021 年 4 月 28 日
編集済み: Matt J 2021 年 4 月 28 日
syms u t
fun=matlabFunction( solve(t^2/64==u,t) );
u2=[1,4,9];
t1=fun(u2)
t1 = 2×3
-8 -16 -24 8 16 24
  1 件のコメント
Tayyab Khalil
Tayyab Khalil 2021 年 4 月 28 日
Yeah that's excellent. Thanks a lot!

サインインしてコメントする。


Matt J
Matt J 2021 年 4 月 28 日
u2 = rand(1,1000);
t1=8*sqrt(u2);
  3 件のコメント
Matt J
Matt J 2021 年 4 月 28 日
All the commands in my solution are Matlab commands...
Tayyab Khalil
Tayyab Khalil 2021 年 4 月 28 日
I meant that only putting the equation in its original form in Matlab and making matlab solve it for t1 rather than separting t1 ourself.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by