0% found this document useful (0 votes)
34K views

Matlab Lab5

This document contains MATLAB scripts and code for various tasks related to a civil engineering lab assignment. It includes scripts to calculate ratios, total costs, absolute values, find maximum values, and conditionally display text. Loops are used to calculate sums and powers of two. Functions and conditionals like if/else, switch statements are used throughout.

Uploaded by

da_masta_j
Copyright
© Attribution Non-Commercial (BY-NC)
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)
34K views

Matlab Lab5

This document contains MATLAB scripts and code for various tasks related to a civil engineering lab assignment. It includes scripts to calculate ratios, total costs, absolute values, find maximum values, and conditionally display text. Loops are used to calculate sums and powers of two. Functions and conditionals like if/else, switch statements are used throughout.

Uploaded by

da_masta_j
Copyright
© Attribution Non-Commercial (BY-NC)
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
You are on page 1/ 10

MATLAB LAB5

CIVL ENG 1B

JOEL FERNANDES 20432971

Scripts
%script for lab4 task_1 %Name: %UserID: Joel Fernandes 20432971

%This script calculates the ratio of to given numbers % Clear the screen first clear % Command to clear all the variables a=input('first number '); b=input('second number '); if b~=0 % '~=' represents 'not equal to' ratio=a/b end

%script for lab4 task_2 %Name: %UserID: Joel Fernandes 20432971

%This script calculates the total cost of a variable amount of apples % Clear the screen first clear % Command to clear all the variables cost = 25; n = input('How many apples do you have in your bag?') if n > 5 totalcost = (1-0.2)*n*cost %cost of n apples after discount else totalcost = n*cost end

function[y]= absval(x) if x>=0 y=x; else y=-x; end

%script for lab4 task_4 %Name: %UserID: Joel Fernandes 20432971

%This script calculates the maximum of any three numbers supplied by the %user num1=input('Please enter the first number: '); num2=input('Please enter the second number: '); num3=input('Please enter the third number: '); if num1 > num2 if num1>num3 max=num1; else max=num3; end elseif num2>num3 max=num2; else max=num3; end disp(max) % this gives the maximum number to be displayed on the screen

%script for lab4 task_5 %Name: %UserID: Joel Fernandes 20432971

%This script conditionally displays different text depending on the value %entered at the command line lotterynumber=input('Enter your number:'); switch lotterynumber case 12 disp('You are a Winner!'); case 24 disp('You are a Winner!'); otherwise disp('Sorry, you lost ... Please play again'); end

%script for lab4 task_6 %Name: %UserID: Joel Fernandes 20432971

n=input('Give me a positive integer.'); x=input('Give me a real number.'); res=0; %declare an initial value fro res for index=1:n res=res+(1+x)^index; end disp(['res=',num2str(res)]);

Command Window first number 3 second number 4 ratio = 0.7500 first number 30 second number 5 ratio = 6 first number 6 second number 0 first number 0 second number 7 ratio = 0 ??? Undefined function or method 'Clear' for input arguments of type 'char'. Error in ==> lab4_task2 at 8 Clear all ??? Undefined function or method 'Clear' for input arguments of type 'char'. Error in ==> lab4_task2 at 8 Clear all the variable values ??? Undefined function or method 'Clear' for input arguments of type 'char'. Error in ==> lab4_task2 at 8 Clear all % Command to clear all the variable values How many apples do you have in your bag?10 n = 10

totalcost = 200 How many apples do you have in your bag?25 n = 25 totalcost = 500 ??? Error: File: lab4_task3.m Line: 14 Column: 1 Function definitions are not permitted in this context. ??? Undefined function or variable "x". Error in ==> absval at 2 if x >= 0 ??? Error: File: absval.m Line: 2 Column: 4 Expression or statement is incomplete or incorrect. ??? Undefined function or variable "x". Error in ==> absval at 2 if x >= 0 >> fplot('absval(x)',[-10 10] ??? fplot('absval(x)',[-10 10] | Error: Expression or statement is incorrect--possibly unbalanced (, {, or [. >> fplot('absval(x)',[-10 10]) ??? Error using ==> inlineeval at 15 Error in inline expression ==> absval(x) Undefined function or variable "x". Error in ==> inline.feval at 36 INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);

Error in ==> fplot at 102 x = xmin; y = feval(fun,x,args{4:end}); >> fplot('absval(x)',[-10 10]) ??? Error using ==> inlineeval at 15 Error in inline expression ==> absval(x) Undefined function or variable 'x'. Error in ==> inline.feval at 36 INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr); Error in ==> fplot at 102 x = xmin; y = feval(fun,x,args{4:end}); >> [10]=absval[-10] ??? [10]=absval[-10] | Error: Unbalanced or unexpected parenthesis or bracket. ??? Undefined function or variable 'x'. Error in ==> absval at 2 if x >= 0 ??? Input argument "x" is undefined. Error in ==> absval at 2 if x >= 0 ??? Input argument "x" is undefined. Error in ==> absval at 2 if x>= 0 ??? Input argument "x" is undefined. Error in ==> absval at 2 if x>=0 ??? Input argument "x" is undefined. Error in ==> absval at 2 if x>=0 >> fplot('absval(x)',[-10 10])

Please enter the first number: 8 Please enter the second number: 8 Please enter the third number: 4 8 Enter your number:12 You are a Winner! >> m=0; >> m=0: for i=1:10 m=[m i]; ??? m=0: | Error: Expression or statement is incomplete or incorrect. >> m=0; for i=1:10 m=[m i]; end m m = 0 10 >> for m=1:10 num=1/(m+1) end num = 0.5000 num = 0.3333 num = 0.2500 num = 1 2 3 4 5 6 7 8 9

0.2000 num = 0.1667 num = 0.1429 num = 0.1250 num = 0.1111 num = 0.1000 num = 0.0909 Give me a positive integer.4 Give me a real number.0.1 res=5.1051 Give me a positive integer.10 Give me a real number.-0.2 res=3.5705 >> for i=1:3 for j=1:3 A(i,j)=1/(i+j-1); end end >> A A =

1.0000 0.5000 0.3333 >> m=0; while m<10 m=m+1 end m = 1 m = 2 m = 3 m = 4 m = 5 m = 6 m = 7 m = 8

0.5000 0.3333 0.2500

0.3333 0.2500 0.2000

m = 9 m = 10 >> n=input('Natural numbers up to ='); s=0; counter=1; while counter<=n s=s+counter; counter=counter+1; end Natural numbers up to =5 >> s s = 15 >> v=1;num=1;i=1; while num<1000 num=2^i; if num>1000 break else v=[v;num]; i=i+1; end end >> v v = 1 2 4 8 16 32 64 128

256 512

You might also like