필터 지우기
필터 지우기

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?

조회 수: 5 (최근 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 CenterFile Exchange에서 Loops 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