Storing x and y values

4 Ansichten (letzte 30 Tage)
Jordan Shook
Jordan Shook am 2 Mai 2020
Kommentiert: Image Analyst am 2 Mai 2020
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.

Antworten (1)

Image Analyst
Image Analyst am 2 Mai 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 Kommentare
Jordan Shook
Jordan Shook am 2 Mai 2020
Here is my whole code:
Essentially I am trying to use Eulers method to get t and y values that I will plot on an x,y graph. I have changed the numbers because this is a school assignement, and although I am not asking for my code to be written, simply just how to store varibles, I would like to make sure that the assignment does not match.
%% Problem 3
%part a euler
t0=1;
tf=5;
y=1;
h=0.5;
n=(tf-t0)/h;
for i=0:0.3:n
dydt=-7*y+t^3;
y=y+dydt*h;
t=t+h;
end
Image Analyst
Image Analyst am 2 Mai 2020
OK, well then you need to index y and y if you want them to change. Use y(i) = ... and t(i) = .....

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by