Storing x and y values
14 views (last 30 days)
Show older comments
Essentially I have a loop as follows:
for i=0:0.5:n
dydx=-47*y+t^3;
y=y+dydx*h;
x=x+h;
end
At each iteration of the loop I would like to store the x and y values that I calculate in a vector, so that I can plot it later on. How would I do this? I tried to add y(i)=y and x(i)=x and I just get back an error warning.
0 Comments
Answers (1)
Image Analyst
on 2 May 2020
When you first enter the loop and get to this line:
dydx=-47*y+t^3;
can you tell us exactly what is y and t at that point?
Perhaps you want something like this but I have no idea:
numPoints = 20;
% Create initial sample data.
x = sort(rand(1, numPoints), 'ascend')
y = sort(rand(1, numPoints), 'ascend')
t = rand(1, numPoints);
h = 5; % Whatever.
for k = 1 : length(y)
dydx = -47*y(k) + t(k) ^3;
y(k) = y(k) + dydx * h;
x(k) = x(k) + h;
end
plot(x, y, 'b.-', 'LineWidth', 2, 'MarkerSize', 20);
grid on;
5 Comments
Image Analyst
on 2 May 2020
OK, well then you need to index y and y if you want them to change. Use y(i) = ... and t(i) = .....
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!