Info

This question is closed. Reopen it to edit or answer.

I need to plot a graph having a linear value in y and another having a quadratic value in y

1 view (last 30 days)
dx = 0.005;
x = dx:dx:500;
m = 25;
velocidad = zeros(1,length(x));
posicion = zeros(1,length(x));
aceleracion = zeros(1,length(x));
F1 = zeros(1,length(x));
for i = 2 : 1 : 500
F1(i) = F1(i)*250; %F1 neds to be a linear value, i had an idea but in the end it just plots a point in cero.
Fmot = F1*sin(x(i));
aceleracion(i) = (Fmot)/m;
velocidad(i) = velocidad(i-1) + aceleracion(i)*dx;
posicion(i) = posicion(i-1) + velocidad(i)*dx;
end
subplot(3,1,1);
plot(x,aceleracion,'.g')
title('Aceleracion')
xlabel('Tiempo')
ylabel('aceleracion')
subplot(3,1,2);
plot(x,velocidad,'.r')
title('Velocidad')
xlabel('Tiempo')
ylabel('velocidad')
subplot(3,1,3);
plot(x,posicion,'.b')
title('Posicion')
xlabel('Tiempo')
ylabel('posicion')
%% fuerza cuadratica
dx = 0.005;
x = dx:dx:500;
m = 25;
F1 = 500;
velocidad = ones(1,length(x));
posicion = ones(1,length(x));
aceleracion = ones(1,length(x));
for i = 2 : length(x)
Fmot = F1*sin(x(i));
aceleracion(i) = (Fmot)/m;
velocidad(i) = velocidad(i-1) + aceleracion(i)*dx;
posicion(i) = posicion(i-1) + velocidad(i)*dx;
F1 = (F1^2)*2; %F1 needs to be a quadratic value
end
subplot(3,1,1);
plot(x,aceleracion,'.g')
title('Aceleracion')
xlabel('Tiempo')
ylabel('aceleracion')
subplot(3,1,2);
plot(x,velocidad,'.r')
title('Velocidad')
xlabel('Tiempo')
ylabel('velocidad')
subplot(3,1,3);
plot(x,posicion,'.b')
title('Posicion')
xlabel('Tiempo')
ylabel('posicion')

Answers (1)

Katie
Katie on 24 Oct 2019
Hi! It looks you have a couple small assignment errors that are preventing your code from working how you want to. Based on your question and the comments in your code, you're only requirement is to plot F1 as a linear function and as a quadratic function. Below are some explanations for what you're seeing as well as examples of changes you could make to meet your requirement.
The reason you're getting all zeros when you plot F1 for the linear function is that you are multiplying 250 by zero for each iteration of your for loop:
F1 = zeros(1,length(x)); %Here you set F1 to be a vector of length 100000 where every element is 0
for i = 2 : 1 : 500
F1(i) = F1(i)*250; %here you're multiplying F1(i) which is 0 by 250, so F1(i)=0*250 for every iteration
Fmot = F1*sin(x(i));
aceleracion(i) = (Fmot)./m;
velocidad(i) = velocidad(i-1) + aceleracion(i)*dx;
posicion(i) = posicion(i-1) + velocidad(i)*dx;
end
To fix this, you could change how you're setting F1 in the for loop. For example, you could multiply x(i) by 250, which would give a linear function.
close all; clear all; clc;
dx = 0.005;
x = dx:dx:500;
m = 25;
F1 = zeros(1,length(x));
for i = 2 : 1 : length(x) %I've changed the limit of the for loop here so x and F1 will be the same size
F1(i) = x(i)*250; %F1 neds to be a linear value, i had an idea but in the end it just plots a point in cero.
Fmot = F1*sin(x(i));
aceleracion(i) = (Fmot(i))./m;
velocidad(i) = velocidad(i-1) + aceleracion(i)*dx;
posicion(i) = posicion(i-1) + velocidad(i)*dx;
end
figure;
plot(x,F1) %this will plot a linear function
For part 2 (plotting the quadratic), right now you're trying to make F1 your quadradic function but it's a constant, so F1=2(F1^2)=2*500*500=500,000. Below is an example of using your x vector to make F1 a quadratic function.
dx = 0.005;
x = dx:dx:500;
m = 25;
Y = 500;
velocidad = ones(1,length(x));
posicion = ones(1,length(x));
aceleracion = ones(1,length(x));
for i = 2 : length(x)
Fmot(i) = Y.*sin(x(i));
aceleracion(i) = (Fmot(i))/m;
velocidad(i) = velocidad(i-1) + aceleracion(i)*dx;
posicion(i) = posicion(i-1) + velocidad(i)*dx;
F1(i) = (x(i)^2)*2; % note that here I'm using F1(i), rather than F1.
% If I were to use F1, I would end up with a 1x1 vector in the
% end because I would be overwriting the value of F1 with every
% loop iteration
end
figure;
plot(x,F1); %this will plot a quadratic function
These pieces of code will run and will give you F1 as a linear function and as a quadratic function.
Note, I didn't do anything with your aceleracion, velocidad, or posicion functions that you're also creating in your loops.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!