Thread Subject: Help on Matlab Program

Subject: Help on Matlab Program

From: Ashok Madan

Date: 11 Mar, 2010 18:15:32

Message: 1 of 4

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

Subject: Help on Matlab Program

From: Jan Simon

Date: 11 Mar, 2010 18:38:21

Message: 2 of 4

Dear Ashok!

> 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
> ...
> 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
> ...

Do you have any specific question? "Any guidelines to start" is very general.

It seems like the function "total" is not related to the problem in any way. But this piece of code appears in "Introduction to Matlab 7 for Engineers"
of William John Palm, page 227.

Kind regards, Jan

Subject: Help on Matlab Program

From: Walter Roberson

Date: 11 Mar, 2010 20:26:44

Message: 3 of 4

Ashok Madan wrote:
> 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

You cannot define a function like that in Matlab. You can, however, define

f = @(x) C0 + C1 .* x;

after which if you apply f(x) with x numeric, the computation would be done
for the x you provide.


Beyond that... examine the documentation for input() and switch.

Subject: Help on Matlab Program

From: Ashok Madan

Date: 13 Mar, 2010 14:27:05

Message: 4 of 4

Walter Roberson <roberson@hushmail.com> wrote in message <hnbk75$u5$1@canopus.cc.umanitoba.ca>...
> Ashok Madan wrote:
> > 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
>
> You cannot define a function like that in Matlab. You can, however, define
>
> f = @(x) C0 + C1 .* x;
>
> after which if you apply f(x) with x numeric, the computation would be done
> for the x you provide.
>
>
> Beyond that... examine the documentation for input() and switch.



Mr. Walter
Regards
Thanks for your time. I new in matlab how to write a function, writing the polynomial equation of that order was just to explain which order equation i used under that question or program. May be I did not explain well enough.
Also the switch and case example which i added was from the book. I am new with Matlab.writing that example was just to show, that is what I found about switch and case program.I just needed a help how to apply this switch and case program command in polynomial regression examples of y = a*x^b
and y = a*exp(b*x.
Thanks again
Ashok

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com