|
I need to write a Matlab program with following requrements
a) Input data from user
b) Perform a regression analysis on polynomials with varying powers
c) on y = a*x^b
d) on y = a*exp(b*x)
use the switch statement
Any guidelines to start or an example
Following is relative work I did
Polynomial of first order
f(x)=C0+C1x
clear
clc
C0=1
C1=1
x=[1,2,3,4,5,6,7];
y=[3,5,7,9,11,13,15];
n=7
A=[n,sum(x);sum(x),sum(x.*x)];
B=[sum(y);sum(x.*y)];
C=A\B
2nd order polynomial
f(x)=C0+C1x+C2x2
clear
clc
C0=1
C1=1
x=[1,2,3,4,5,6,7];
y=[2,5,10,17,26,37,50];
n=7
A=[n,sum(x),sum(x.*x);sum(x),sum(x.*x),sum(x.*x.*x);sum(x.*x),sum(x.*x.*x),sum(x.*x.*x.*x)];
B=[sum(y);sum(x.*y);sum(x.*x.*y)];
C=A\B
Non-linear equation
Y=f(x)=axb
Using log
Log(y)=Log(a)+b*Log(x)
Rewrite I form Y=A+BX
Which can be represented as
f(x)=C0+C1x
A=[n,sum(x);sum(x),sum(x.*x)];
B=[sum(y);sum(x.*y)];
C=A\B
To find a and b apply inverse transformation
C0=A=Log(a)
C0=Logea
a=e C0
C1=B=b
Then b= C1
Another Non-linear equation
Y=f(x)=aebx
Using log
Loge(y)=Loge(a)+b*x*Loge(e)
Rewrite I form Y=A+BX
Which can be represented as
f(x)=C0+C1x
A=[n,sum(x);sum(x),sum(x.*x)];
B=[sum(y);sum(x.*y)];
C=A\B
To find a and b apply inverse transformation
C0=A=Log(a)
C0=Logea
a=e C0
C1=B=b
Then b= C1
In poly regression fit an equation like
Y=k1ek2x which is similar to Y=f(x)=aebx
a=k1 and b=k2
Apply log
Loge(y)=Loge(a)+b*x*Loge(e)
Rewrite I form Y=A+BX
Using given data and apply inverse
Transformation and following program
We can vaues of ‘C’
clear
clc
x=[0,1,2,2.5,3]
y=[2,5.2,14.5,23.5,37.5]
Y=log(y)
M=[5,sum(x);sum(x),sum(x.*x)]
RHS=[sum(Y);sum(x.*Y)]
C=M\RHS
function total_days = total(month,day,extra_day)
total_days = 1
extra_day=1
for k = 1:12 - 1
switch k
case {1,3,5,7,8,10,12}
total_days = total_days + 31
case {4,6,9,11}
total_days = total_days + 30
case 2
total_days = total_days + 28 + extra_day;
end
end
|