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?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponses (1)
VBBV
le 10 Avr 2023
Modifié(e) : VBBV
le 10 Avr 2023
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 commentaires
VBBV
le 10 Avr 2023
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
le 10 Avr 2023
If you are looking for a loop but also separate output variable names, then although it is technically possible, we highly recommend against that.
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!