You are on page 1of 4

%name and age exercise

Name=input('Your name?','s');
Age=input('Your age?');
fprintf('%s will be %d this year\n',Name,Age);

1
%exercise 5.1 no 1
F1=input('Force 1?');
x1=input('displacement 1?');
k1=(F1/x1);
fprintf('The spring constant for test 1 is %d\n',k1)
E1=((1/2)*k1*(x1)^2);
fprintf('The potential energy in test 1 is %d\n',E1)
F2=input('Force 2?');
x2=input('displacement 2?');
k2=(F2/x2);
fprintf('The spring constant for test 2 is %d\n',k2)
E2=((1/2)*k2*(x2)^2);
fprintf('The potential energy in test 2 is %d\n',E2)
F3=input('Force 3?');
x3=input('Displacement 3?');
k3=(F3/x3);
fprintf('The spring constant for test 3 is %d\n',k3)
E3=((1/2)*k3*(x3)^2);
fprintf('The potential energy in test 3 is %d\n',E3)
ave_E=((E1+E2+E3)/3);
fprintf('The average value of potential energy is %d\n',ave_E)

1
%exercise 5.1 no 2
function [E_ave]=exercise_5_1_2(x1,F1,x2,F2,x3,F3)
k1=(F1/x1);
k2=(F2/x2);
k3=(F3/x3);
E1=((1/2)*k1*(x1)^2);
E2=((1/2)*k2*(x2)^2);
E3=((1/2)*k3*(x3)^2);
E_ave=(E1+E2+E3)/3
end

1
%exercise 5.1 no 3
function [E1,E2,E3,E_ave]=exercise_5_1_3(x1,F1,x2,F2,x3,F3)
k1=(F1/x1);
k2=(F2/x2);
k3=(F3/x3);
E1=((1/2)*k1*(x1)^2)
E2=((1/2)*k2*(x2)^2)
E3=((1/2)*k3*(x3)^2)
E_ave=(E1+E2+E3)/3
end

You might also like