help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Affecting new value to a sym


From: Doug Stewart
Subject: Re: Affecting new value to a sym
Date: Sun, 23 Dec 2018 18:58:21 -0500



On Sun, Dec 23, 2018 at 5:53 PM Skyweird <address@hidden> wrote:
Hi, I hope you guys are doing well.

The final goal is to calculate the maximal error between an exact solution
(u(x) in my script) and the approximation/polynomial form I got (using the
least squares method).

Long story short, I had to find the minimum of ||Ax-b||² (I calculated A and
I have b). So, as asked, I used the least squares method and found my vector
x.

The values of my vector x are the coefficients of the polynomial form I have
to make. To get this polynomial form, I used the symbolic package and the
function "poly2sym" to get it (it worked !).

But now, my x are "symbolic" and I have to evaluate my polynomial form for
x=0:0.001:1. *How can I make this happen, like, making my x usable in a for
loop ?*

First I tried to do it without the symbolic, so I can use my x in a for
loop. But then, for each value of x (a thousand), it remakes the entire
calculation to find the vector x, which isn't what I'm looking for and takes
forever. So I thought the solution may be with symbolic ?


Here is what I did without using symbolic :

function c=c(n,M)                                             #this function
gives me my coefficients
  [A,b]=MomentFlechissant(n,M);                       #MomentFlechissant
gives me my matrix A and vector b
  x=MC(A,b);                                                    #MC is my
least squares method giving me x
  n=size(x);
  c=[];
  for i=0:n-1   
  c(i+1)=x(i+1);
  end
end

function p=p(x)       #this function gives me my polynomial form
  c=c(5,4);              #5 and 4 represents the size of my matrix A
  n=size(c);
  N=n(2);
  p=0;
  for i=0:N-1
    p=p+c(i+1)*x.^(i);
  end
end

function u=u(x)      #this is the exact solution

u=1/2*(exp(-x)*exp(1))/(exp(-1)-exp(1))-1/2*(exp(x)*exp(1))/(exp(-1)-exp(1))-1/2*x.*exp(x);
end

and this is the error calculation :
V=[];
for k=0:1000
  v=abs(p(k/1000)-u(k/1000));     #with p my polynomial form and u the exact
solution
  V(k+1)=v;
end
max(V);

=> but with that script, it calculates the coefficient c again and again for
each value of x, which are the same.

________________________________________________________________________________________

I hope my issue isn't too complicated to understand (it's my first project
on programming).
Thanks a lot and I wish you happy holidays.

Fiona




--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html




look at the second example and see if it helps you.
https://wiki.octave.org/Symbolic_package


--
DASCertificate for 206392


reply via email to

[Prev in Thread] Current Thread [Next in Thread]