フィルターのクリア

make loop if two variable (x and y) present calculate value for (x1 and y1) and store separate name for same formula another calculate for (x2,y2) and store by separate?

3 ビュー (過去 30 日間)
x = [1 2 3 4 5];
y= [ 8 3 2 1 6];
z = exp(x)+x*y^3 +y;
calculate it and store as
z1(x1,y1)
z2(x2,y2)
...so on..
and
plot(x1,z1)
hold on
plot(x2,z2)
hold on
plot(x3,z3)
.....
hold off

回答 (1 件)

VBBV
VBBV 2023 年 4 月 10 日
編集済み: VBBV 2023 年 4 月 10 日
x = [1 2 3 4 5];
y= [ 8 3 2 1 6];
% vectorize the equation (easier approach)
z = exp(x)+x.*y.^3 +y;
% using for loop
hold on
for k = 1:numel(z)
z(k) = exp(x(k))+x(k)*y(k)^3 +y(k);
plot(x(k),z(k),'ro','MarkerSize',10,'linewidth',2);
end
figure
plot(x,z)
  5 件のコメント
VBBV
VBBV 2023 年 4 月 10 日
Can this be made in a loop?
Yes, that i what has been shown in below code
x = [1 2 3 4 5];
y= [ 8 3 2 1 6];
hold on
for k = 1:numel(x)
z(k) = exp(x(k))+x(k)*y(k)^3 +y(k); % as an array
plot(x(k),z(k),'ro','MarkerSize',10,'linewidth',2);
end
figure
hold on
for k = 1:numel(x)
% without array (only scalar z !) as you wanted
z = exp(x(k))+x(k)*y(k)^3 +y(k); % z will have only last value at end of loop !!!
plot(x(k),z,'bo','MarkerSize',10,'linewidth',2);
end
Walter Roberson
Walter Roberson 2023 年 4 月 10 日
If you are looking for a loop but also separate output variable names, then although it is technically possible, we highly recommend against that.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by