WK 3

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Write a program to display the results (Pass/Fail) of an exam using

input, if, elseif and disp commands


• 75≤Grade≤ 100  Passes with distinction
• 60≤Grade<75  Passes with first class
• 45≤Grade<60  Passes with second class
• 0≤Grade<45  Fails
• Other scores are not valid.
Pseudocode
Clear console
Clear functions & variables
Ask Grade as input from user
If Grade > 100 then write «Invalid grade»
Else if Grade ≥ 75 then write «Passes with distinction»
Else if Grade ≥ 60 then write «Passes with first class»
Else if Grade ≥ 45 then write «Passes with second class»
Else if Grade ≥ 0 then write «Fails»
Else if write «Invalid grade»
End if
Scilab code
clc; //Clear console
clear; //Clear functions & variables
grade=input('Enter the grade : '); //Ask Grade as input from user
if grade> 100 then disp("Invalid grade");
elseif grade>= 75 then disp("Passes with distinction");
elseif grade>= 60 then disp("Passes with first class");
elseif grade>= 45 then disp("Passes with second class");
elseif grade>= 0 then disp("Fails");
else disp("Invalid grade");
end
Consider two functions f and g defined over the interval −2≤𝑥≤5 with 50
points. Plot these two functions f(𝑥) = (𝑥2+2𝑥)e-𝑥 𝑎𝑛𝑑 𝑔(𝑥)= 𝑠𝑖𝑛(𝑥/2)

Pseudocode
Clear console
Clear functions & variables
Define function f(𝑥) = (𝑥2+2𝑥)e-𝑥
Define function g(𝑥) = 𝑠𝑖𝑛(𝑥/2)
Assign x the interval −2≤𝑥≤5 with 50 points
Clear figure
Plot f(𝑥) & g(𝑥)
Scilab code
clc; //Clear console
clear; //Clear functions & variables
//Define function f(𝑥) = (𝑥2+2𝑥)e-𝑥
function y=f(x);
y=(x^2+2*x)*exp(-x);
endfunction
//Define function g(𝑥) = 𝑠𝑖𝑛(𝑥/2)
function y=g(x);
y=sin(x/2);
endfunction
//Assign x the interval −2≤𝑥≤5 with 50 points
x=linspace(-2,5,50)
clf; //Clear figure
//Plot f(𝑥) & g(𝑥)
plot(x,f,"r",x,g,"g")
Plot of sequence of points using for command. u(n)=(-0.8)n

Pseudocode
Clear console
Clear functions & variables
For n from 1 to 50
u(n) takes the value (-0.8)n
End for
Plot n and u(n)
Scilab code
clc; //Clear console
clear; //Clear functions & variables
//For n from 1 to 50
//u(n) takes the value (-0.8)^n
//End for
for n=1:50
u(n)=(-0.8)^n
end
//Plot u(n)
plot(u,"*r")
Determine the future value of a deposit both simple interest and
compound interest defining function in the SCILAB program. Plot a
graph with properly labelled legends and axis (x-y) between the future
values by simple and compound interest versus the time if the USD
100,000 is deposited in the bank with an interest rate of 9%.
Pseudocode
Clear console
Clear functions & variables
Define function to calculate interest and principal sum
where si=p+p*r*t/100
and ci=p*(1+r/100)t
Assign p the principal sum 100000$
Assign r the interest rate 9%
Assign t time 1 to 10
Calculate interest rates and assign them to y
Plot time versus y
Put legends, title and labels
Scilab code
clc; //Clear console
clear; //Clear functions & variables
//Define function to calculate interest and principal sum
//where si=p+p*r*t/100
//and ci=p*(1+r/100)^t
function y=interest(p, r, t)
si=p+p*r*t./100
ci=p.*(1+r/100)^t
y=[si ci]
endfunction
p=100000; //Assign p the principal sum 100000$
r=9; //Assign r the interest rate 9%
t=[1:10]'; //Assign t time 1 to 10
//Calculate interest rates and assign them to y
y=interest(p,r,t)
//Plot time versus y
plot(t,y)
//Put legends, title and labels
xlabel('time in years')
ylabel('Amount, USD of SI,CI')
legend('SI','CI')
xtitle('Comparison of SI and CI for $100000')
xgrid(5,1)

You might also like