Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Extracting Coefficients from differential equations

Subject: Extracting Coefficients from differential equations

From: Mirage

Date: 18 Oct, 2012 14:39:09

Message: 1 of 3

Hi,

I'm would like to know if there is a matlab function that will extract the coefficients from a differential equation.

E.g.
y = 24.42x3 - 0.87x2 + x + 489

I want to extract the numbers 24.42, -0.87, 1, and 489.

Thanks

Subject: Extracting Coefficients from differential equations

From: Richard Crozier

Date: 18 Oct, 2012 19:55:12

Message: 2 of 3

"Mirage " <miragel@uvic.ca> wrote in message <k5p4ad$11d$1@newscl01ah.mathworks.com>...
> Hi,
>
> I'm would like to know if there is a matlab function that will extract the coefficients from a differential equation.
>
> E.g.
> y = 24.42x3 - 0.87x2 + x + 489
>
> I want to extract the numbers 24.42, -0.87, 1, and 489.
>
> Thanks

How about?

polystr = 'y = 24.42x3 - 0.87x2 + x + 489'

matchstr = '(?<=[\s+\-])x'

newpolystr = regexprep(polystr, matchstr, '1')

matchstr = '(?<!x)([-+]?[0-9]*\.?[0-9]+)((?=x)|(?=\b))'

[S,E,TE,M] = regexp(newpolystr, matchstr)

for i = 1:numel(M)

  coeff(i) = str2double(M{i});

end

coeff

Subject: Extracting Coefficients from differential equations

From: Ryan

Date: 29 Oct, 2012 19:03:08

Message: 3 of 3

I thought I just answered this question a few minutes ago. Here it is again!!

str = '24.42x3 - 0.87x2 + x + 489(*&'; %# Your sample string
alphaStr = str(isstrprop(str,'alpha')); %# Get the alphabetic characters
digitStr = str(isstrprop(str,'digit')); %# Get the numeric characters
otherStr = str(~isstrprop(str,'alphanum')); %# Get everything that isn't an
                                             %# alphanumeric character



str = '24.42x3 - 0.87x2 + x + 489'
idx = regexp(str,'\d+')
nums = regexp(str,'\d+','match')
%%To get the numbers out, as opposed to strings, use:
str2num(nums{1})


"Mirage " <miragel@uvic.ca> wrote in message <k5p4ad$11d$1@newscl01ah.mathworks.com>...
> Hi,
>
> I'm would like to know if there is a matlab function that will extract the coefficients from a differential equation.
>
> E.g.
> y = 24.42x3 - 0.87x2 + x + 489
>
> I want to extract the numbers 24.42, -0.87, 1, and 489.
>
> Thanks

Tags for this Thread

No tags are associated with this thread.

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.

Contact us