Programmering
SVAR til Introduction to programming and data processing - Matlab
Hej,
Dette er ikke et spørgsmål men blot en hjælp til andre, da jeg har set disse opgaver delvist løst på diverse fora. Jeg har derfor valgt at uploade alle mine obligatoriske opgaver fra dette kursus her som en samlet pakke, som forhåbentlig kan hjælpe fremtidige studerende.
Kom gerne med forslag til mere effektive og kortere løsninger i kommentarerne og ellers god fornøjelse.
_______________________
1F)
WhatISay='Hello CodeJudge!';
_______________________
1G)
b=12;
c=10;
A=0.25*pi;
a=sqrt(b^2+c^2-2*b*c*cos(A));
_______________________
1H)
a=2;
b=-5;
c=2;
x1=(-b-sqrt(b^2-4*a*c))/(2*a);
x2=(-b+sqrt(b^2-4*a*c))/(2*a);
_______________________
Herefter hedder filerne deres funktioner - Det er derfor vigtigt at trykke "Run" og lade filen hedde hvad funktionen er. Hvis du er usikker på hvilken opgave koden tilhører kan du blot søge efter funktionen.
_______________________
function y=evaluateTaylor(x);
y=(x-1)-1/2*(x-1)^2+1/3*(x-1)^3;
_______________________
function projection = computeProjection(a)
x=length(a);
b=ones(1,x);
l=0;
for i=1:x
l=l+a(i)^2;
end
projection=dot(a,b)/(l)*a;
_______________________
function A=boxArea(boxCorners,area)
x1=boxCorners(1);
x2=boxCorners(2);
x3=boxCorners(3);
x4=boxCorners(4);
y1=boxCorners(5);
y2=boxCorners(6);
y3=boxCorners(7);
y4=boxCorners(8);
A1=(x2-x1)*(y2-y1);
A2=(x4-x3)*(y4-y3);
A0=max(0,min(x2,x4)-max(x1,x3))*max(0,min(y2,y4)-max(y1,y3));
switch area
case 'Box1'
A=A1;
case 'Box2'
A=A2;
case 'Intersection'
A=A0;
case 'Union'
A=A1+A2-A0;
end
_______________________
function theta=acuteAngle(v1,v2)
Sigma=acos(dot(v1,v2));
if Sigma<(pi/2)
theta=Sigma;
elseif Sigma>(pi/2)
theta=pi-Sigma;
end
_______________________
function gdistance=gravitationalPull(x)
R=6.371*10^6;
g0=9.82;
if x>=R
gdistance=g0*(R^2/x^2);
elseif (0 <= x) && (x <= R)
gdistance=g0*(x/R);
end
_______________________
function category=pH2Category(pH)
if (0 <= pH) && (pH < 3)
category=('Strongly acidic');
elseif (3 <= pH) && (pH < 6)
category=('Weakly acidic');
elseif (6 <= pH) && (pH <= 8)
category=('Neutral');
elseif (8 < pH) && (pH <= 11)
category=('Weakly basic');
elseif (11 < pH) && (pH <= 14)
category=('Strongly basic');
elseif pH < 0 || pH > 14
category=('pH out of range');
end
_______________________
function tN = bacteriaGrowth(n0, alpha, K, N)
tN = 0;
n = n0;
n_prev = n0;
while n < N;
n = (1 + alpha*(1 - n_prev / K )) * n_prev;
tN = tN + 1;
n_prev = n;
end
_______________________
function averageRate = fermentationRate(measuredRate,lowerBound,upperBound)
valid = measuredRate > lowerBound & measuredRate < upperBound;
averageRate = mean(measuredRate(valid));
end
_______________________
function idComplete=removeIncomplete(id)
x = floor(id);
keep = false(1,length(x));
for i = unique(x)
keep(x==i) = sum(x==i)==3;
end
idComplete = id(keep);
_______________________
function T=convertTemperature(T,unitFrom,unitTo)
Celsius='Celsius';
Fahrenheit='Fahrenheit';
Kelvin='Kelvin';
if strcmpi(unitFrom,Celsius) && strcmpi(unitTo,Fahrenheit)
T=1.8*T+32;
elseif strcmpi(unitFrom,Celsius) && strcmpi(unitTo,Kelvin)
T=T+273.15;
elseif strcmpi(unitFrom,Fahrenheit) && strcmpi(unitTo,Celsius)
T=(T-32)/1.8;
elseif strcmp(unitFrom,Fahrenheit) && strcmp(unitTo,Kelvin)
T=(T+459.67)/1.8;
elseif strcmp(unitFrom,Kelvin) && strcmp(unitTo,Celsius)
T=T-273.15; elseif strcmp(unitFrom,Kelvin) && strcmp(unitTo,Fahrenheit)
T=1.8*T+459.67;
end
_______________________
function A = circleAreaMC(xvals, yvals)
N = length(xvals);
n = sum(xvals.^2 + yvals.^2 < 1);
A = 4*n/N;
_______________________
function itemCost = computeItemCost(resourceItemMatrix, resourceCost)
n = size(resourceItemMatrix, 2);
if n >= 1 && n <= 4
itemCost = resourceCost(:).' * resourceItemMatrix;
else
error('Unexpected input.');
end
end
_______________________
function E=computeLanguageError(freq)
letter=readtable('letter_frequencies.csv');
letter = letter{1:26,2:16};
E=zeros(1,15);
for i=1:15
E(i) = sum((freq-letter(:,i)').^2);
end
_______________________
function freq=letterFrequency(filename)
Letter='abcdefghijklmnopqrstuvwxyz';
text=fileread(filename);
text=lower(text);
v=zeros(1,26);
for i=1:26
v(i)=sum(text==Letter(i));
end
freq=v;
freq=v/sum(v)*100;
_______________________
function ysmooth=movingAvg(y)
n=length(y);
m=zeros(5,n+4);
w = [1 2 3 2 1];
for i=1:5
m(i,i:i+n-1) = y*w(i);
end
s=(sum(m)./9);
ysmooth = s(3:(n+2));
_______________________
function natoText = textToNato(plainText);
str = upper(plainText);
natalph = ["Alpha","Bravo","Charlie","Delta","Echo","Foxtrot","Golf", ...
"Hotel","India","Juliet","Kilo","Lima","Mike","November","Oscar", ...
"Papa","Quebec","Romeo","Sierra","Tango","Uniform","Victor",...
"Whiskey","Xray","Yankee","Zulu"];
noralpha = ['A' : 'Z'];
natoText='';
for i = 1:length(str)
for j = 1:26
if str(i)==noralpha(j)
natoText=strcat(natoText,'-',natalph(j));
end
end
end
natoText=eraseBetween(natoText,1,1);
Rigtig god fornøjelse!
Søgeord:
Contents 1 Introduction to programming 1 1.1 Aims and objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.3 The integrated development environment interface . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Exercise 1A: Getting to know the interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Exercise 1B: Simple arithmetic operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Exercise 1C: Help on functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Exercise 1D: Mathematical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Exercise 1E: Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Assignment 1F: Hello CodeJudge! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Assignment 1G: The cosine rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Assignment 1H: The quadratic formula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2 Functions, vectors, and testing 9 2.1 Aims and objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.2 Functions and scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Assignment 2A: Taylor series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3 Working with vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Exercise 2B: Initializing and indexing vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Exercise 2C: Vector operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Exercise 2D: Logical vector indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Exercise 2E: Modifying vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.4 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Assignment 2F: Projection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Assignment 2G: Box area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Optional challenge 2H: Sudoku row . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3 Selection statements 29 3.1 Aims and objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.2 Using selection statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Exercise 3A: True or false . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Exercise 3B: Logical expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.3 Comparison pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Assignment 3C: Angle between lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Assignment 3D: Piecewise function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Assignment 3E: Acidity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Optional challenge 3F: Football goal tracker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4 Looping 41 4.1 Aims and objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.2 For- and while-loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 4.3 Loop pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 4.4 Loops and vectorized computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 4.5 Displaying formatted output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Exercise 4A: Repeated printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Exercise 4B: Power series approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Exercise 4C: Square roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Assignment 4D: Fermentation rate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Assignment 4E: Bacteria growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Assignment 4F: Removing incomplete experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Optional challenge 4G: Cluster analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 iii Contents 5 Computer programs 55 5.1 Aims and objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 5.2 Writing useful comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Assignment 5A: Temperature conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 5.3 Input from the user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Exercise 5B: Interactive temperature calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Exercise 5C: A simple menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 5.4 Creating an interactive program main script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 5.5 Computer simulation with random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Exercise 5D: Random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Assignment 5E: Monte Carlo estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Optional challenge 5F: Thermodynamic simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 6 Files and matrices 71 6.1 Aims and objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 6.2 Files and directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 6.3 Loading and saving variables in files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Exercise 6A: Loading and saving variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 6.4 Working with matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Assignment 6B: Production cost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Assignment 6C: Moving average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 6.5 Reading text files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Assignment 6D: Frequency of letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 6.6 Working with CSV files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Assignment 6E: Language detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Exercise 6F: Advanced file types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 7 Plotting and strings 91 7.1 Aims and objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 7.2 Working with plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Exercise 7A: Cassiopeia graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Exercise 7B: Scatter plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Exercise 7C: Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Exercise 7D: Radiocarbon dating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Exercise 7E: Temperature in the UK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Exercise 7F: Saving and printing plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 7.3 Working with strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Assignment 7G: The NATO alphabet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Skriv et svar til: SVAR til Introduction to programming and data processing - Matlab
Du skal være logget ind, for at skrive et svar til dette spørgsmål. Klik her for at logge ind.
Har du ikke en bruger på Studieportalen.dk?
Klik her for at oprette en bruger.