MatLab HW1 CH4-6
MatLab HW1 CH4-6
-- 40843245
preface
info
Reference Book
CH4-6
[Code]
%MatLab HW CH4-6
%HW Date:2021/10/22
%clear workspace
clear
%clear command window
clc
%create a symbolic variable
x=sym ('x');
C(x)=0.1*x^2+7*x+210;
vec1=[1:2:30];
%ans
y=C(vec1)
[Ans]
y=
[2171/10, 2319/10, 495/2, 2639/10, 2811/10, 2991/10, 3179/10, 675/2, 3579/10, 3791/10, 4011/10, 4239/10, 895/2, 4719/10, 4971/10]
CH4-26
[Code]
%MatLab HW CH4-26
%HW Date:2021/10/22
%clear workspace
clear
%clear command window
clc
%input data
strdata=["1","Computer","English","Chinese","Math";
"1","93","89","90","82";
"2","90","95","88","87";
"3","78","84","81","82";
"4","89","89","90","82";
"5","93","92","90","88";
"6","89","90","88","78";
"7","85","87","83","83"];
%convert string matrix to cell matrix
celldata=cellstr(strdata)
[strdata_numRow,strdata_numCol]=size(strdata);
%numRow mean how many student are there?
%I have to subtract 1 since first row of strdata is subject.
numRow=strdata_numRow-1;
%create table of student scores.
intdata=strdata(2:numRow,:)
%{
function [total,avg]=totalCell(A,startRow,endRows,startCols,endCols)
for idx=startRow:endRows
total(idx)=sum(A(idx))
avg(idx)=total(idx)/(endRow-startRow)
end
end
cellfun(@a totalCell(a,2,numRow,2,6),intdata)
%}
[Ans]
celldata =
8×5 cell array
{'1'} {'Computer'} {'English'} {'Chinese'} {'Math'}
{'1'} {'93' } {'89' } {'90' } {'82' }
{'2'} {'90' } {'95' } {'88' } {'87' }
{'3'} {'78' } {'84' } {'81' } {'82' }
{'4'} {'89' } {'89' } {'90' } {'82' }
{'5'} {'93' } {'92' } {'90' } {'88' }
{'6'} {'89' } {'90' } {'88' } {'78' }
{'7'} {'85' } {'87' } {'83' } {'83' }
CH5-8
[Code]
%MatLab HW CH5-8
%HW Date:2021/10/22
%clear workspace
clear
%clear command window
clc
%create a symbolic variable
t=sym ('t');
F(t)=1200/(1+28*exp(-0.2*t));
%(a)ans
result1=F(10)
%(b)ans
vec1=10:5:50
result2=F(vec1)
[Ans]
[1200/(28*exp(-2) + 1), 1200/(28*exp(-3) + 1), 1200/(28*exp(-4) + 1), 1200/(28*exp(-5) + 1), 1200/(28*exp(-6) + 1), 1200/(28*exp(-7) + 1), 1200/(28*exp(-8) + 1), 1200/(28*exp(-9) + 1), 1200/(28*exp(-10) + 1)]
CH5-17
[Code]
%MatLab HW CH5-17
%HW Date:2021/10/22
%clear workspace
clear
%clear command window
clc
%way1:
%solve it
syms x y z;
equ1=3*x-3*y+z==2
equ2=x-y+z==1
equ3=7*x+5*y-z==-5
%find a solution of equation
sol=solve([equ1,equ2,equ3],[x,y,z])
%ans
x=sol.x
y=sol.y
z=sol.z
[Ans]
sol =
struct with fields:
x: -1/6
y: -2/3
z: 1/2
x =
-1/6
y =
-2/3
z =
½
[Screenshot]
CH6-11
[Code]
%MatLab HW CH6-11
%HW Date:2021/10/22
%clear workspace
clear
%clear command window
clc
%way1
%original way - for loop
result=1
n=1001
for idx=1:n:1
result=result*((-1)^idx)
end
result
%way2
%simplified math formula
%since //(-1)^2=1,(-1)^3=(-1),...
%ans=1 when n is even, ans=-1 when n is odd.
n=1001
%ans
result=mod(n,2)*2-1
[Ans]
result =
1
心得
從寫作業中學到許多東西,其中也包括老師上課沒教到的東西了。
如:CH5-17
我學到了如何用程式解方程式。
我google了一 下發現matlab很強大,只要一行就可得到答案。
(先寫好方程式,透過solve函式得到結構解)
Comments
Post a Comment