How to differentiate a series of functions denoted by varying a parameter i=1,2 in (h_i)?

1 回表示 (過去 30 日間)
I typed the following code and command window displays unrecognised function h_i. If I remove the outer loop and replace "i" with some number, say "2", then it displays unrecognised function x_j. What's the mistake?
clc
clear all
syms x_1 x_2
h_1(x_1,x_2)=x_1+(x_2)*(x_1);
h_2(x_1,x_2)=x_2+x_1*x_2^2;
for i=1:2
for j=1:2
diff(h_i,x_j)
end
end

採用された回答

darova
darova 2020 年 2 月 23 日
編集済み: darova 2020 年 2 月 23 日
Mistake that h_1 and h_i and different variables
m1stake = 2;
m2stake = 3;
for i = 1:2
disp(mistake) % doesn't work
end
Use arrays and cells
clc
clear all
syms x_1 x_2
h_1(x_1,x_2)=x_1+(x_2)*(x_1);
h_2(x_1,x_2)=x_2+x_1*x_2^2;
X = {x_1 x_2};
H = {h_1 h_2};
for i=1:2
for j=1:2
diff(H{i},X{j})
end
end

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 2 月 23 日
Don't use a loop. For just 4 cases, just write them out
h_1(x_1,x_2) = x_1 + (x_2) * (x_1);
h_2(x_1,x_2) = x_2 + x_1 * x_2^2;
diff(h_1,x_1)
diff(h_1,x_2)
diff(h_2,x_1)
diff(h_2,x_2)
  3 件のコメント
Image Analyst
Image Analyst 2020 年 2 月 23 日
You don't have 56 separate and differently named variables, do you? If so, you really need to learn how to use arrays.
Asit Srivastava
Asit Srivastava 2020 年 2 月 24 日
I have 8 functions in 7 variables. Yeah, I am learning MATLAB.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by