Value of /TaylorMaclaurin Series/ graph problem - octave

The right side of the expression must equal the left side. Later I have to draw a graph for both functions. Can someone help me?
This is my code
clc
clear all
x=[6,7,8,9,10,11,12,13,14]
a = zeros(1,9);
b = zeros(1,9);
for i=1:9
b(i)=((6*x(i))/((2+x(i)).*(4-x(i))));
a(i)=3*x(i)
for n=2:12
a(i)=a(i)+((1/(4^n))-(((-1)^n)/(2^n))).*(x(i)^n);
end
end
figure(1);
plot(x, b, "color", "r")
hold on
plot(x, a, "color", "b")
legend("exact function", "approximate function")
this is what i have to change into a code

Related

OCaml meaning of "in"

Lets assume the following function
val foo : int -> int -> bool -> int
let foo x y b =
let x,y = if x > y then y,x else x,y in
let rec loop x y b =
if x >= then x
else if b then loop (x+1) y (not b)
else loop x (y-1) (not b)
in
loop x y b
I still don't quite understand the concept of the "in".
Does the line mean "let x,y = ... in" that it is executed immediately or only when you "Call" it? And when i dont need to call it why do i need the last line loop x y b?
Thanks in advance :)
in is just part of the OCaml syntax - let PATTERN = EXPR1 in EXPR2 is an expression which binds the result of EXPR1 to PATTERN and then evaluates EXPR2 with the new bindings present. In some languages like F# and Haskell, you don't (always) need in - it's inferred from the indentation. OCaml syntax is indentation insensitive which requires it to have an explicit in.
Does the line mean "let x,y = ... in" that it is executed immediately or only when you "Call" it?
It's evaluated immediately.
And when i dont need to call it why do i need the last line loop x y b?
In this code, the previous line defines a function named loop with 3 arguments, and then later you call the function with the arguments x y b.

scilab - how to return matrices from a function with if-statements?

I have a scilab function that looks something like this (very simplified code just to get the concept of how it works):
function [A, S, Q]=myfunc(a)
A = a^2;
S = a+a+a;
if S > A then
Q = "Bigger";
else
Q = "Lower";
end
endfunction
And I get the expected result if I run:
--> [A,S,Q]=myfunc(2)
Q =
Bigger
S =
6.
A =
4.
But if I put matrices into the function I expect to get equivalent matrices back as an answer with a result but instead I got this:
--> [A,S,Q]=myfunc([2 4 6 8])
Q =
Lower
S =
6. 12. 18. 24.
A =
4. 16. 36. 64.
Why isn't Q returning matrices of values like S and A? And how do I achieve that it will return "Bigger. Lower. Lower. Lower." as an answer? That is, I want to perform the operation on each element of the matrix.
Because in your program you wrote Q = "Bigger" and Q = "Lower". That means that Q will only have one value. If you want to store the comparisons for every value in A and S, you have to make Scilab do that.
You can achieve such behavior by using loops. This is how you can do it by using two for loops:
function [A, S, Q]=myfunc(a)
A = a^2;
S = a+a+a;
//Get the size of input a
[nrows, ncols] = size(a)
//Traverse all rows of the input
for i = 1 : nrows
//Traverse all columns of the input
for j = 1 : ncols
//Compare each element
if S(i,j) > A(i,j) then
//Store each result
Q(i,j) = "Bigger"
else
Q(i,j) = "Lower"
end
end
end
endfunction
Beware of A = a^2. It can break your function. It has different behaviors if input a is a vector (1-by-n or n-by-1 matrix), rectangle matrix (m-by-n matrix, m ≠ n ), or square matrix (n-by-n matrix):
Vector: it works like .^, i.e. it raises each element individually (see Scilab help).
Rectangle: it won't work because it has to follow the rule of matrix multiplication.
Square: it works and follows the rule of matrix multiplication.
I will add that in Scilab, the fewer the number of loop, the better : so #luispauloml answer may rewrite to
function [A, S, Q]=myfunc(a)
A = a.^2; // used element wise power, see luispauloml advice
S = a+a+a;
Q(S > A) = "Bigger"
Q(S <= A) = "Lower"
Q = matrix(Q,size(a,1),size(a,2)) // a-like shape
endfunction

administer drug dose at certain time step in octave

I am doing a simple task on ocatve.
I have to administer drug dose at day 1,7,21 and 28..
i wrote a function like that:
function xdot = f (x,t)
a=[1;7;21;28]
drug=0; ### initially drug is zero
if (t==a)
drug=drug+57.947;
else
drug=drug+0;
endif
xdot=(-0.4077)*(x)+ drug; passing the value of drug to differential equation
endfunction
In the main file i called this function in lsode:
t=linspace(0,30,30);
x0=0;
y=lsode(#ex,x0,t); ### ex is the file name where function is written
plot(t,y,'o')
This program doesn't work.. it displays all the time zero value for drug. Can anybody help me that how to administer dug dose with certain time step by manipulating linspace function.
It looks like you have a simple clearance model, and at each time in a, you want the dose to be delivered instantaneously. That is, at each time in a, the amount of drug in the subject increases by 57.947.
If that is the model that you have in mind, implementing it in the formula for xdot will not work well. You would actually need to implement it as a "delta function", and lsode won't work with that.
Instead, I recommend solving the problem in stages, corresponding to the time intervals [0, 1], [1, 7], [7, 21], and [21, 28]. During one stage, the differental equation is simply xdot = -0.4077*x. In the first stage, the initial condition is 0. In the next stage, the initial condition is the final value of the previous stage plus the dosage amount 57.947.
Here's a script:
dose = 57.947;
a = [1 7 21 28 30];
x0 = 0.0;
t0 = 0;
t = [];
y = [];
for t1 = a
tinterval = linspace(t0, t1, 2*(t1 - t0) + 1);
yinterval = lsode(#(x, t) -0.4077*x, x0, tinterval);
t = [t tinterval];
y = [y yinterval'];
t0 = t1;
x0 = yinterval(end) + dose;
end
plot(t, y, "linewidth", 2);
The script creates this plot:
Note that the differential equation xdot = -k*x has the solution x0*exp(-k*(t-t0)), so the call to lsode could be replaced with
yinterval = x0*exp(-0.4077*(tinterval - t0));
If you do this, also remove the transpose from yinterval two lines below:
y = [y yinterval];
If you want to keep the implementation of the administration of the drug dose within the formula for xdot, you'll need to distribute it over a small time interval. It could be implemented as a short rectangular pulse of width w and height 57.974/w. You'll also need to ensure that lsode takes sufficiently small internal time steps (less than w) so that it "sees" the drug dose.
You probably want replace t==a by ismember (t, a). Also, you should erase the else clause as it has no effect on the answer.
UPDATE
Consider rewriting your function as:
function xdot = f (x,t)
xdot = -0.4077*x + 57.947*ismember (t, [1 7 21 28])
endfunction

Octave integrating

I have some problems with integrating in Octave.
I have the following code:
a=3;
function y = f (x)
y = x*x*a;
endfunction
[v,ier,nfun,err]=quad("f",0,3);
That 'a' in function is giving me troubles.
Octave says that 'a' is undefined. So, if I instead of 'a' put number 3 in function y everything works just fine. However, I want to have 'a' in function so I can change it's value.. How do I do that?
Thanks
You could use a function closure, which will encapsulate the a.
function f = makefun (a)
f = #(x) x * x * a;
endfunction
f = makefun(3)
[v, ier, nfun, err] = quad(f, 0, 3);
There are two main options.
Option 1 is, as voithos notes, make 'a' an input to the function.
Option 2 is to define 'a' to be a global variable.
global a=3;
function y = f (x)
global a
y = x*x*a;
endfunction
[v,ier,nfun,err]=quad("f",0,3);
This will cause 'a' to be the same value inside and outside the function.
Your function is actually dependent on two values, x and a, therefor:
f=#(x,a) x*x*a
[V, IER, NFUN, ERR] = quad (#(x) f(x,3), A, B, TOL, SING)
I used inline functions as i think it is easier to understand.

Error plotting a function of 2 variables

I am trying to plot the function
f(x, y) = (x – 3).^2 – (y – 2).^2.
x is a vector from 2 to 4, and y is a vector from 1 to 3, both with increments of 0.2. However, I am getting the error:
"Subscript indices must either be real positive integers or logicals".
What do I do to fix this error?
I (think) I see what you are trying to achieve. You are writing your syntax like a mathematical function definition. Matlab is interpreting f as a 2-dimensional data type and trying to assign the value of the expression to data indexed at x,y. The values of x and y are not integers, so Matlab complains.
If you want to plot the output of the function (we'll call it z) as a function of x and y, you need to define the function quite differently . . .
f = #(x,y)(x-3).^2 - (y-2).^2;
x=2:.2:4;
y=1:.2:3;
z = f( repmat(x(:)',numel(y),1) , repmat(y(:),1,numel(x) ) );
surf(x,y,z);
xlabel('X'); ylabel('Y'); zlabel('Z');
This will give you an output like this . . .
The f = #(x,y) part of the first line states you want to define a function called f taking variables x and y. The rest of the line is the definition of that function.
If you want to plot z as a function of both x and y, then you need to supply all possible combinations in your range. This is what the line containing the repmat commands is for.
EDIT
There is a neat Matlab function meshgrid that can replace the repmat version of the script as suggested by #bas (welcome bas, please scroll to bas' answer and +1 it!) ...
f = #(x,y)(x-3).^2 - (y-2).^2;
x=2:.2:4;
y=1:.2:3;
[X,Y] = meshgrid(x,y);
surf(x,y,f(X,Y));
xlabel('x'); ylabel('y'); zlabel('z');
I typically use the MESHGRID function. Like so:
x = 2:0.2:4;
y = 1:0.2:3;
[X,Y] = meshgrid(x,y);
F = (X-3).^2-(Y-2).^2;
surf(x,y,F);
xlabel('x');ylabel('y');zlabel('f')
This is identical to the answer by #learnvst. it just does the repmat-ing for you.
Your problem is that the function you are using uses integers, and you are trying to assign a double to it. Integers cannot have decimal places. To fix this, you can make it to where it increases in increments of 1, instead of 0.2