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

6 vues (au cours des 30 derniers jours)

clear;clc;
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

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
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (1)

VINAYAK LUHA le 1 Déc 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
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur 変換 dans Help Center et File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!