150 likes | 159 Views
This helpful assistant provides functions to find the maximum and minimum of two numbers, solve quadratic equations, generate random numbers, calculate textual qualification for a given numerical mark, calculate final grades, perform matrix operations, and use various built-in MATLAB functions.
E N D
Conditionals: functionwhichreturnsmax and min in rightorder • function [max, min] = maxmin(x, y) • max = (x+y)/2 + abs(x-y)/2 • min = (x+y)/2 - abs(x-y)/2 • End • Short butnot “natural”, thefollowingismuch more comprehensible • function[max, min] = maxmin(x, y) • if x > y • max = x; min = y; • else • max= y; min = x; • endif • end
Nowwe can be more general in solvingthequadraticequation function [ re1, im1, re2, im2 ] = ec2( a, b, c ) % solves a quadratic equation % Input : % a, b, c: coefficients of equation a*x^2+b*x+c=0 % Output: % re1,im1,re2,im2: real and imaginary parts of the solution % Use: [re1, im1, re2, im2] = ec2(a, b, c) d=b^2-4*a*c; if d>=0 re1=(-b+sqrt(d))/(2*a); re2=(-b-sqrt(d))/(2*a); im1=0; im2=0; else re1=-b/(2*a); re2=re1; im1=sqrt(-d)/(2*a); im2=-im1; end
Usingit >> [r1,i1,r2,i2] = ec2(1,-1,-1) r1 = 1.6180 i1 = 0 r2 = -0.6180 i2 = 0 >> [r1,i1,r2,i2] = ec2(1,-1,1) r1 = 0.5000 i1 = 0.8660 r2 = 0.5000 i2 = -0.8660
If and generatingrandomnumbers % Generate a random number between 1 and 100 a = randi(100); % If it is even, divide by 2 if rem(a, 2) == 1 disp('a is odd') a = a+1; endif b = a/2 % Generate a random number between 0 and 0.999999 a = rand(); if a < 0.5 disp(‘head') else disp(‘tail') endif
Example: The “Examen de grado” in UCH • Dependingonthenumerical grade received in the final examstudentsfromthe Universidad de Chile get a textual qualification in their diploma. Grades gofrom 1.0 (worst) to 7.0 (best). Thenumericl grade G istranslated to textaccording to thefollowing rules: • If G islessthan 4 you do notgetyour diploma • If G isbetween 4 and 4.9 yougetan “aprobado”, • IfG isbetween 5 and 5,9 yougetan “aprobado con distinción” • If G is 6.0 orhigheryougetan “aprobado con distinción máxima”. • Write a script whichreadsthenumericalmark and printsthecorresponding textual qualification
Examen de grado: Solution 1 g = input(“input numerical grade"); if (g < 4) disp(“Reprobado”) else if(g < 5) disp(“Aprobado”) else if(g < 6) disp(“Aprobado con distincion”) else disp(“Aprobado con distincionmaxima”) end end end
Examen de grado: Solution 2 n = input(“input numericalmark"); if (n < 4) disp(“Reprobado”) elseif(n < 5) disp(“Aprobado”) elseif(n < 6) disp(“Aprobado con distincion”) else disp(“Aprobado con distincionmaxima”) end
RemembertheexcerciseforExecl ? • Calculating the final grade of a hypothetical course reading the Test average grade (T), the Assignment average grade (A) and the grade received for participation in class (P). • If both averages T and A are >= 5 then the final status is calculated by considering 50% the test average, 30% the Assignments and 20% the participation • If the average for the tests is less than 5 then the final status should show “no pass” and the final mark is not calculated. • If the average of the tests is >= 5 but the average of the assignments is < 5 then if the participation mark is >= 8 or the average test is >= 8 then the final status is calculated as in 2. Otherwise the final status should show the word “Incomplete” • T = input("Tests average "); • A=input("Assignment average "); • P=input("Participation") • if T >= 5 && A >= 5 • sprintf("Your final grade is %f",T*0.5+A*0.3+P*0.2) • elseif T < 5 • sprintf("No pass") • elseif T >= 8 || P >= 8 • sprintf("incomplete") • endif
Matrices • Transposition: Exchange rows for columns • >> M = [5 3 -1; 0 -2 1] • M = • 5 3 -1 • 0 -2 1 • >> M' • ans = • 5 0 • 3 -2 • -1 1
Elements of a Matrix • Theelement in row i and column j of a matrix A • Isidentified as A(i,j) • >> M = [5 3 -1; 0 -2 1] • 3 -1 • 0 -2 1 • >> M(2,3) • ans = 1 • >> k=1; • >> M(k+1, k*2-1) • ans = 0
Dimensions of a Matrix • >> M = [5 3 -1; 0 -2 1]; • >> dim= size(M) • dim = 2 3 • >> [rows, cols]= size (M) • rows = 2 • cols = 3
A simple example • % crete a matrix S 4x4 withtheupperrighttrianglewith • % includingthe diagonal; therestshould be zeroes • % 1 1 1 1 • % 0 1 1 1 • % 0 0 1 1 • % 0 0 0 1 • S = zeros(4,4); • for i= 1:4 • for j= i:4 • S(i,j) = 1; • end • end
Slices • FromthefollowingMatrix A 2x3: • A = [ 1 2 3 ; 4 5 6 ] • We can to getthesecondrowdoing: • A (2, 1:3) • ans • 4 5 6
silces(2) • Gettingthethirdcolumn: • A (1:2, 3) • ans • 3 • 6
Funciones vistas en la preparación • ForthefollowingMatrix B: • B = [1 2 3; 4 5 6; 7 8 9] • sum(B) • ans = • 12 15 18 • diag(B) • ans = • 1 • 5 • 9