Qiming
Posts:
10
Registered:
12/21/12
|
|
Re: get coefficients from a symbolic polynomial
Posted:
Jan 5, 2013 5:49 PM
|
|
"Qiming " <qzfyc@mst.edu> wrote in message <kca9p0$na1$1@newscl01ah.mathworks.com>... > "Nasser M. Abbasi" wrote in message <kca7tr$o64$1@speranza.aioe.org>... > > On 1/5/2013 3:49 PM, Qiming wrote: > > > > > > > > Hi Nasser: > > > > > > Thanks for your reply. > > > > > > In your case, actually only 'x' is symbolic, the coefficients 0,1,2,3 are already > > >in numerical form. But my question is: what if 0,1,2,3 are all in symbolic forms? > > > > > > I only give a simple example so that you might think why can't I > > >directly put 0,1,2,3 in the expression. However, in my real application, > > >those coefficients long and complicated and are generated by MATLAB symbolic > > >tool box. I just want to "retrieve" those coefficients for my future use. Of > > >course I can check those coefficients from the command window, but I > > >don't want to put them back in my code by hand. > > > > > > Qiming > > > > > > > why is it so hard for someone to just make a simple example to explain > > what they mean? one spends so much time writing words, instead of > > making a simple code example. > > > > If you mean this: > > > > EDU>> syms x a b > > EDU>> y=a*x^2+b*x+1; > > > > then do > > > > EDU>> coeffs(y,x) > > [ 1, b, a] > > > > If you still mean something else, then make up a small example. Do not > > explain in words, show a small Matlab example of the input polynomial > > itself. complete and self contained example. I am sure you can make > > one up if you try. > > > > --Nasser > > > > > > > > Nasser: > > Okay, let me explain my questions more in details with an example. > > What I'm trying to do is to test the Galerkin's method, the following is the part of the code where I have an issue: > > ------------------------------------------------------------------------------------------------------ > clc; > close all; > clear all; > > NumOrder = 2; % approximation order > > t0 = 0; tf = 1; % time span for integration > > syms x c1 c2; > y_x = c1*x + c2*x^2; % trial function, 2nd order polynomial > dy_x = diff( y_x, x ); > residual_error = dy_x + y_x - 1; % residual error > > % Take the inner product of residual error and basis function > IP(1) = int( residual_error*x, t0, tf ); > IP(2) = int( residual_error*x^2, t0, tf ); > ... > ... > ... > ------------------------------------------------------------------------------------------------------ > > If just run the code to where I provide, the MATLAB gives: > IP(1) = (5*c1)/6 + (11*c2)/12 - 1/2 > IP(2) = (7*c1)/12 + (7*c2)/10 - 1/3 > What I need is the coefficient of IP, i.e., > 5/6, 11/12, -1/2 and > 7/12, 7/10, -1/3 > c1 and c2 are the variables I need to solve. > I want to construct a matrix A with the coefficents: > A = [ 5/6, 11/12; 7/12, 7/10 ] > and a vector b with > b = [ 1/2; 1/3 ]; > After that, I can solve c1 and c2 by A*c=b, or c = inv(A)*b, where c = [c1;c2]. > That's what I really want to do. > > Hope this time it's clear. > > Qiming
Nasser:
Okay, I got it. As you suggested, I add one line to get those coefficients:
coefficients(1,:) = double(coeffs(IP(1)));
Then I have numerical coefficients.
Qiming
|
|