0% found this document useful (0 votes)
10 views10 pages

MATLAB Functions for Calculations

Uploaded by

danieljaid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

MATLAB Functions for Calculations

Uploaded by

danieljaid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

% %2

% current_hour=input('The time is:')

% function next_hr=nexthour(current_hour)

% if current_hour==12

% next_hr=1

% else

% next_hr=current_hour+1

% end

% end

% next_hr=nexthour(current_hour)

% fprintf('The next hours is %d\n',next_hr)

% %7

% employment_2016=input('The employment in 2016 is: ')

% employment_2017=input('The employment in 2017 is: ')

% if employment_2016>employment_2017

% fprintf('There was a decrease\n')

% else

% fprintf('There was an increase\n')

% end

% %8

% a=input('The value of a is: ')

% c=input('The value of c is: ')

% if a<=0||c<=0 || c<=a

% fprintf('error')

% end

% function b = findb(a,c)

% b = sqrt(c^2 - a^2);

% end
% b = findb(a,c)

% fprintf('The value of b is:%d\n',b)

% %13

% A1=input('The area of A1 is:')

% A2=input('The area of A2 is:')

% if A1==A2

% fprintf('The velosity will be the same\n')

% elseif A1>A2

% fprintf('The velosity will increase\n')

% else

% fprintf('The velosity will decrease\n')

% end

%Chap5

%%6

% function loop=sumsteps(n)

% loop=0

% for i=1:2:n

% loop=loop+i

% end

% end

% loop=sumsteps(11)

%8

% number=randi([2,5])

% total=0

% for i=1:number

% user_input=input('Enter ther number:\n')

% total=total+user_input

% fprintf('The sum is %.1f\n',total)

% end
% %15

% for i=1:5

% for j=1:i

% fprintf('%6d',i*j)

% end

% fprintf('\n')

% end

% %21

% myprod = 1;

% i = 1;

% for i=4 && i<4

% num = input('Enter a number: ');

% myprod = myprod * num;

% i = i + 1;

% end

%23

% n=1;

% exp=0.36787;

% approximation= (1-(1/n))^n;

% value= exp-approximation;

% while abs(value)>=0.0001

% n=n+1

% approximation= (1-(1/n))^n;

% end

% fprintf('The value of exp:%.4f\n',exp)

% fprintf('the value of the approximation is:%.4f\n',approximation)

% fprintf('The numbweer of n needed:%f\n',n)

% %27
% function prtemps()

% max_celsius=input('Write a value between -16 to 20:\n')

% while max_celsius<-16|| max_celsius>20

% fprintf('error')

% max_celsius=input('Write a value between -16 to 20:\n')

% end

% fprintf('%6s,%6s\n','F','C')

% for F=0:5:1000

% C=(5/9)*(F-32)

% if C>max_celsius

% fprintf('error')

% break

% end

% fprintf('%6.1f,%6.1f\n',F,C)

% end

% %28

% while true

% user_input=input('>','s');

% if user_input==('q')

% disp('Exiting the command line');

% break

% end

% fprintf('You entered: %s\n', user_input);

% end

% %#23

% n=1

% exp=0.3679

% approximation= (1-(1/n))^n
% value=exp-approximation

% while abs(exp-approximation)>=0.0001

% n=n+1

% approximation= (1-(1/n))^n

% end

% fprintf('The value of e:%.4f\n',exp)

% fprintf('The value of approximation:%.4f\n',approximation)

% fprintf('The values of n required are:%.4f\n',n)

%28

% while true

% n=input('>','s')

% if strcmp(n,'q')

% disp('Exiting command line.');

% break

% end

% fprintf('You entered:%s\n',n)

% end

%Chap6

% %4

% function [positive,negative]=splitm(vec)

% positive=vec(vec>=0)

% negative=vec(vec<0)

% end

% vec=[-10,5,8,-4,7,-9]

% [positive,negative]=splitm(vec)

% disp('The positive numbers are:')


% disp(positivenumbers)

% disp('The negative numbers are:')

% disp(negativenumbers)

% %14

% figure(1)

% x=1:10:60

% y=1:10:60

% plot(x,y,'r*:')

% xlabel('time')

% ylabel('time2')

% mat = [4 2 4 3 2; 1 3 1 0 5; 2 4 4 0 2];

% [r, c] = size(mat);

% totalSum=0

% for i = 1:r

% totalSum=sumprint(mat(i,:),totalSum);

% end

% function totalSum = sumprint(row, totalSum)

% rowSum = sum(row);

% totalSum = totalSum + rowSum;

% fprintf('The sum is now %d\n', totalSum); % Print the cumulative sum

% end

% %2

% function [miles,naustical]=convertkilometer(k)

% miles=k*0.621;

% naustical=k/1.852;

% end
% K = input('Enter a value of k:'); % Distance in kilometers

% fprintf('Distance in miles: %.2f\n', miles);

% fprintf('Distance in US nautical miles: %.2f\n', naustical);

% [miles, naustical] = convertkilometer(K);

% %4

% function [positivenumbers,negativenumbers]=splitem(vec)

% positivenumbers= vec(vec>=0)

% negativenumbers=vec(vec<0)

% end

% vec=[-10,-3,5,4,9,-7];

% [positivenumbers,negativenumbers]=splitem(vec);

% disp('The positive numbers are:')

% disp(positivenumbers)

% disp('The negative numbers are:')

% disp(negativenumbers)

% %14

% function inches=cubit2inch(cubit)

% inches=cubit*18

% end

% n=input('The number of cubit:')

% inches=cubit2inch(cubit)

% fprintf('The number of inches:%d\n',inches)]

% %17

function [r,theta,phi]=convert2spher(x,y,z)

r=sqrt(x^2+y^2+z^2);

if r==0
theta=0;

else

theta=acos(z/r);

end

if x==0 && y==0

phi=0;

else

phi=atan(y/x);

end

end

function printspherical(x, y, z)

[r, theta, phi] = convert2spher(x, y, z);

fprintf('The radius is %.2f\n', r);

fprintf('The inclination angle is %.2f\n', theta);

fprintf('The azimuth angle is %.2f\n', phi);

end

x=5;y=6;z=8;

printspherical(x, y, z)

midterm

% A=25/5-2;

% B=10-2^3;

% C=[5 -1 0];

% D=[2 6 3.5];

% E=C*D';

% user_number=input('Please enter an integer greater than 0 and smaller than 5: ');


% if user_number==E && user_number>A

% disp('Hello World!')

% else

% switch user_number

% case 1

% Rec_perimeter=R_perimeter(user_number,B)

% case 2

% fprintf('The user typed in the value %d\n',user_number)

% case 3

% user_number=user_number+3^3

% otherwise

% G=ones(B,A)^2

% end

% end

% function perimeter=R_perimeter(a,b)

% % This function calculates the perimeter of a rectangle using input 1

% % and input 2 as rectangle's width and length.

% perimeter=(a+b)*2;

% end

%5

% function new_matrix = matrix_fun(matrix)

% % Get the size of the matrix

% [rows, cols] = size(matrix);

% % Initialize the new matrix with the same size

% new_matrix = zeros(rows, cols);

% % Loop through each element in the matrix


% for i = 1:rows

% for j = 1:cols

% % Check if the element is non-negative

% if matrix(i, j) >= 0

% % Calculate the square root of non-negative elements

% new_matrix(i, j) = sqrt(matrix(i, j));

% else

% % Calculate the absolute value of negative elements

% new_matrix(i, j) = abs(matrix(i, j));

% end

% end

% end

% % Save the new matrix to a .dat file

% save('new_matrix.dat', 'new_matrix', '-ascii');

% % Return the new matrix

% return;

% end

You might also like