Clear Filters
Clear Filters

vpasolveで解​いた方程式の解がない​場合の代入

4 views (last 30 days)
泰誠 平山
泰誠 平山 on 20 Aug 2023
Answered: VINAYAK LUHA on 1 Dec 2023
下記のコードではdataに入っている14行のデータから3行のデータを取り出してeqの式に代入し,その時のp0,p1,Deltaの解を求め,Kに格納する全組み合わせの計算を行っています。しかし,組み合わせの中には一部解がないものが混じっており,「左辺のサイズが 1x1 で右辺のサイズが 0x1 であるため、代入を実行できません。」とコマンドウインドウに表示されて計算が途中でストップしてしまいます。解がないものがあっても計算が止まらないようにしたいです。これが実現できるコードをご教授頂きたいです。何卒宜しくお願い致します。
clear;clc;
data=readmatrix(filename);
C=nchoosek(1:length(data(:,1)),3); %全てのデータの行の組み合わせ
K=zeros(length(C(:,1)),6); %p0,p1,Δ,の格納場所
for i=1:length(C(:,1))
data1=data(C(i,1),1:2);%1点目
data2=data(C(i,2),1:2);%2点目
data3=data(C(i,3),1:2);%3点目
%3式の2次方程式の計算
syms p0 p1 Delta
sigma = [1/data1(1,1); 1/data2(1,1); 1/data3(1,1)];
Hv = [data1(1,2); data2(1,2); data3(1,2)];
eq = p0+p1*Delta*sigma-(p1*Delta.^2/3)*sigma.^2 -Hv== 0;
result=vpasolve(eq); % solve(eq)
%p0,p1,Δ,の格納
K(i,1)=getfield(result,"p0");
K(i,2)=getfield(result,"p1");
K(i,3)=getfield(result,"Delta");
P0=K(i,1);
P1=K(i,2);
D=K(i,3);
end
下記にdataに入っている数値を記載しています。 14×2
2.3 171
3.5 164
4.4 156
5.1 153
5.7 148
7.2 140
8.4 134
9 131
10 126
11.2 123
12.3 119
12.7 118
14 115.5
15.4 114

Answers (1)

VINAYAK LUHA
VINAYAK LUHA on 1 Dec 2023
Hi 泰誠 平山,
私の母国語は日本語ではないので、この質問には英語で答えます。ご理解のほどよろしくお願いいたします。.
I understand that you aim to numerically solve symbolic equations. You are creating 14C3 combinations of your data, and for each combination, solving for the symbolic variables using MATLAB "vpasolve" function and further storing the solutions in an array.
However, due to certain combination lacking solutions, the "vpasolve" halts the calculation with an error. Hence, you are seeking a solution to ensure that calculations continue even when no solutions are available for a combination.
As a solution, you can include a conditional check on the "result" variable to determine if any of the solution for symbolic variables are empty. In the event that they are empty, which occurs when no solution is possible, store "NaN" for such solutions in the solution array "K".
Here is the modified code snippet based on the above logic-
clear;clc;
data = [
2.3 171;
3.5 164;
4.4 156;
5.1 153;
5.7 148;
7.2 140;
8.4 134;
9 131;
10 126;
11.2 123;
12.3 119;
12.7 118;
14 115.5;
15.4 114
];
C = nchoosek(1:length(data(:,1)),3); % All combinations of rows in the data
K = zeros(length(C(:,1)),6); % Storage for p0, p1, Δ
for i = 1:length(C(:,1))
data1 = data(C(i,1),:); % 1st point
data2 = data(C(i,2),:); % 2nd point
data3 = data(C(i,3),:); % 3rd point
% Calculation of the 3-equation 2nd degree
syms p0 p1 Delta
sigma = [1/data1(1,1); 1/data2(1,1); 1/data3(1,1)];
Hv = [data1(1,2); data2(1,2); data3(1,2)];
eq = p0 + p1*Delta*sigma - (p1*Delta^2/3)*sigma.^2 - Hv == 0;
result = vpasolve(eq); % solve(eq)
% Storage of p0, p1, Δ
if (isempty(result.p0) || isempty(result.p1) || isempty(result.Delta))
K(i,1) = NaN;
K(i,2) = NaN;
K(i,3) = NaN;
else
K(i,1) = double(result.p0);
K(i,2) = double(result.p1);
K(i,3) = double(result.Delta);
end
end
Hope this helps you in understanding how to modify your code such that the flow proceeds to completion.
Additionally, refer to the following article for more details about how to use conditional statements in MATLAB.
Regards,
Vinayak Luha

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!