필터 지우기
필터 지우기

how to create a vector

조회 수: 1 (최근 30일)
Abdullah Jizani
Abdullah Jizani 2016년 5월 16일
편집: Abdullah Jizani 2016년 5월 26일
Hello everyone, I hope you are doing well. I need some help with my matlab project. I''m finding the velocity and time after each 1 meter displacement. It''s good when I''m doing that and I''m actually getting correct results. However, I couldn''t find the velocity and time after for example 1.5 or 1.6 meter. this is my program, so can you please help me with it.
function [v,t]=freefall(h)
g=9.81;
%Gravity in m^2/s
for k=1:h
v(k)=sqrt(2*g*k);
end
for
k=1:h
t(k)=sqrt(2*k/g);
end
plot(t,v)
xlabel(''Time
(s)'')
ylabel(''Velocity (m/s)'')
title(''Free fall: velocity vs time'')

채택된 답변

Star Strider
Star Strider 2016년 5월 16일
Use the interp1 function.
If I understand your code correctly, this will work. Add these lines to the end of your code:
k = 1:h; % Displacement
dspl_intrp = [1.5; 1.6]; % Displacements To Interpolate
vt_intrp = interp1(k, [v' t'], [1.5; 1.6], 'linear'); % Interpolate Velocity & Time
fprintf(1, 'Displacement %.2f, Velocity = %.2f, Time = %.2f\n', [dspl_intrp vt_intrp]')
Displacement 1.50, Velocity = 5.35, Time = 0.55
Displacement 1.60, Velocity = 5.53, Time = 0.56
I added the fprintf call to show that the result appears to be correct.
  댓글 수: 4
Abdullah Jizani
Abdullah Jizani 2016년 5월 16일
but it shows me this error ??? Attempted to access v(1.1); index must be a positive integer or logical.
Error in ==> freefall at 7 v(k)=sqrt(2*g*k);
Star Strider
Star Strider 2016년 5월 16일
I forgot that you used it as an index.
This works:
g=9.81; %Gravity in m^2/s
d = 1 : 0.1 : h; % Displacement Vector
for k=1:length(d)
v(k)=sqrt(2*g*d(k));
end
for k=1:length(d)
t(k)=sqrt(2*d(k)/g);
end
plot(t,v)
xlabel('Time (s)')
ylabel('Velocity (m/s)')
title('Free fall: velocity vs time')
Note that here you have to change the ‘k’ reference as the independent variable in your calculations to ‘d(k)’. This also gives you flexibility to make ‘d’ anything you want, even negative or zero, because ‘k’ is now derived from it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by