Plotting the graph of only the last iteration

1 visualización (últimos 30 días)
Mathew
Mathew el 17 de Feb. de 2025
Respondida: Walter Roberson el 17 de Feb. de 2025
% Need to plot the graph of only the last iteration but multiple graphs are
% produced. Only one graph is desired.
clear all; close all;
a=0.3; M=1; r0=1; r1=1; p=0.5; d=0.5; Z=10; t=0:1:100;
H=@(t) (p*Z-d-exp(-t))*r1;
syms T U
for n=1:10
y=int(H(U),0,T);
yy= r0 + ((1-a)./M)*H(t).*r1 + (a./M)*y;
r1=yy;
end
fplot(0.5.*exp(a.*t).*yy(10),[0,100])
xlabel 'Time t'
ylabel 'Graph of iteration'
grid on

Respuesta aceptada

Walter Roberson
Walter Roberson el 17 de Feb. de 2025
syms T U
for n=1:10
y=int(H(U),0,T);
yy= r0 + ((1-a)./M)*H(t).*r1 + (a./M)*y;
r1=yy;
end
The output in y is symbolic -- necessarily so since it includes the unresolved variable T as the upper bound of integration.
t=0:1:100;
H=@(t) (p*Z-d-exp(-t))*r1;
t is a numeric vector. H is defined as an anonymous function in which all of the components are numeric constants except for the input named t, so when H() is invoked with a numeric vector t as it is in the yy assignment line, the H(t) will be numeric. All of the components in yy are numeric except for the y which is necessarily symbolic, so the result yy will be symbolic. There will be one entry in yy for each entry in H(t) which in turn will have one entry for each component of t
Each iteration of your for n loop calculates exactly the same thing, as you are not accumulating any variables and none of the iterations involve the loop control variable n . So at the end of the for n loop, yy will be a symbolic vector involving symbolic variable T, with one entry in the vector for each entry in t
fplot(0.5.*exp(a.*t).*yy(10),[0,100])
You select the 10'th entry from the symbolic vector yy, which will be a single expression involving the symbolic variable T
You multiply that 10th entry by a calculation involving the entire numeric vector t so the end result of the calculation is a symbolic vector of length the same as t, involving the symbolic variable T
fplot() is being asked to plot that vector of length same as t with symbolic variable T in each component. So it does so, producing 101 graphs...

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by