Taylor series method expansion
20 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
function sol=taylor1(fn,dfn,a,b,y0,n)
h=(b-a)/n;
x=a+(0:n)*h;
y(1)=y0;
for k=1:n
y(k+1)=y(k)+h*feval(fn,x(k),y(k)) + (h^2*feval(dfn,x(k),y(k)))/2;
end
sol=[x',y'];
the above function working for the functions with single variable 'x', how can we evaluate for two variables x and y. Giving error as
>> taylor1(f,df,0,0.8,1.5,5)
Error using feval
Argument must contain a character vector or function handle.
Error in taylor1 (line 8)
y(k+1)=y(k)+h*feval(fn,x(k),y(k)) + (h^2*feval(dfn,x(k),y(k)))/2;
2) how can we expand the series for more number of terms 3) how can we use vectors instead of for loop
13 comentarios
PJS KUMAR
el 18 de Sept. de 2018
Editada: Walter Roberson
el 19 de Sept. de 2018
Walter Roberson
el 19 de Sept. de 2018
"one vector should contain the values of f(x0), f'(x0), f''(x0), f'''(x0),...."
"second vector should contain the values of 1/0!, (x-x0)/1!, (x-x0)^2/2!, (x-x0) ^3/3!,"
There are some non-loop possibilities from there. Assuming they are row vectors and not column vectors:
- sum(first_vector .* second_vector)
- first_vector * second_vector.'
In the case of real valued functions, you can also do
- dot(first_vector, second_vector)
Respuestas (2)
PJS KUMAR
el 20 de Sept. de 2018
Editada: Walter Roberson
el 20 de Sept. de 2018
1 comentario
Walter Roberson
el 20 de Sept. de 2018
Order n:
function yb=taylor(f,a,b,ya,n)
syms x y;
h=(b-a)/n;
y(1)=ya;
y(2)=f;
ht=h.^(0:n)./factorial(0:n)
for i=2:n
y(i+1)=diff(y(i),x)
end
for i=1:n
x=a+i*h;
c=eval(y);
yb=sum(c.*ht);
fprintf('The value of %f is %f\n',x,yb);
end
end
2) The code already handles functions in multiple variables, as long as the variable of differentiation is x. To change that you would have to pass in the variable of differentiation and use that instead of x in your diff() call. Notice, though, that whatever your a, b, and ya parameters are supposed to mean, they have been constructed to only deal with a single dimension, not to deal with the possibility of expansion around a 2D point or in a circle or square.
Reminder: taylor series are always only with respect to one variable at a time. In some contexts it can make sense to extend taylor series to multiple dimensions around a point. In such case the way to proceed is to take the taylor series with respect to a single variable at a time.
3) There is no way to ask for differentiation to a number of different degrees simultaneously, only to ask for differentiation to one particular degree. It is therefor not possible to vectorize the differentiation process. The closest you could get would be to use arrayfun() to hide the differentiation loop, which would probably involve recalculating the same derivative many times unless you took care to "memoize" the calculation.
4) Your code will fail no matter what the data type is that you give for ya and f. The closest you could get would be if you specified both ya and f as symbolic, then you could get as far as the eval(). However, eval of a symbolic array can fail, because it means the same thing as eval() of char() of the symbolic array except replacing matrix() and array() calls with better syntax, and char() of symbolic entries can involve syntax that is not MATLAB and is not MuPAD either. You should never eval() a symbolic expression.
PJS KUMAR
el 21 de Sept. de 2018
3 comentarios
Walter Roberson
el 21 de Sept. de 2018
You have a conflict between the variable named y in your code in the form
y(1)=ya;
y(2)=f;
and the y in your f=@(x)x^2+y^2, which refers to some variable that you must have defined before you called the function. The conflict exists even if you had used
syms y
f = @(x)x^2+y^2
After all, look at what you are saying: you have
y = [ 3/2, x^2 + y(x)^2, 2*x + 2*y(x)*diff(y(x), x), 2*diff(y(x), x)^2 + 2*y(x)*diff(y(x), x, x) + 2, 2*y(x)*diff(y(x), x, x, x) + 6*diff(y(x), x)*diff(y(x), x, x), 2*y(x)*diff(y(x), x, x, x, x) + 6*diff(y(x), x, x)^2 + 8*diff(y(x), x)*diff(y(x), x, x, x)]
When you eval() this, y is the vector shown, so y(x) is asking to index the vector at the location given by x, which you have defined as x=a+i*h which is unlikely to work out as an exact integer.
If you had used some other variable name instead of y(1) and so on, then y in the eval() would instead refer to
syms x y;
which would be a scalar, and you would have the related problem of trying to index a scalar symbol at a non-integer location (in particular at a location that did not happen to exactly equal 1)
We can tell from the output you show that you must have done
syms y(x)
before you defined
f = @(x)x^2+y^2
But is it really true that y is a function of x, that the expression to be taylor'd is x^2 + y(x)^2 ?
Anyhow, diff(y(x), x, x) when x is numeric and y(x) is symbolic is not defined. In such a case, diff(y(x), x) when x is numeric and y(x) is symbolic is defined, but only for the case where x happens to be a non-negative integer, in which case it is the derivative number. And if y(x) is numeric instead of symbolic and x is numeric, then diff(y(x), x, x) is only valid if x is a nonnegative integer dimension number (third parameter) and is simultaneously a nonnegative integer designating the order difference (second parameter).
Remember I posted above that you should never eval() a symbolic expression? This is an example of what happens: you end up mixing symbolic expression syntax with MATLAB syntax, and you get Fail City.
Elavarasi E
el 28 de Sept. de 2019
am too working on a similar kind of problem. can u guide me if ur code has worked
Ver también
Categorías
Más información sobre Number Theory en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!