how can I deal with Problem on Graph in Matlab? - matlab

Question:
Consider the Initial value problem $$x'(t) = t*x^2$$ and $x(0)=1$
Solve this problem using ode45 for $ 0 \le t \le 1 $ and plot the result. And try solving again for $ 0 \le t \le 2 $
My matlab code for t in [0, 1] is as follows:
function f = fun( t, x )
f = t*x.^2;
end
>> [tv1 f1] = ode45 ('fun', [0 1], 1);
>> plot(tv1, f1)
And I get the following graph
However, when I run this Matlab code for t in [0, 2]
function f = fun( t, x )
f = t*x.^2;
end
>> [tv2 f2] = ode45 ('fun', [0 2], 1);
>> plot(tv2, f2)
I get the following graph,
Why this last graph for t in [0, 2] is wrong? How can I correct it?
Many thanks for your helps!

Related

MatLab input argument error with objective function and fmincon()

I am working through an example using fmincon().
I define my objective function in objFun.m
function f=objFun(x)
f = 100*(x(2) - (x(1))^2)^2 + (1 - x(1))^2;
end
and I define an initial point x0
x0=[1; -1]
And if I run the objective function with that point as a test I get
>> objFun(x0)
ans =
400
But when I try to use it in fmincon() I get
>> [x, fval] = fmincon(objFun, x0, [1;2],1,[],[],[0; -inf],[inf, 0]);
Not enough input arguments.
Error in objFun (line 2)
f = 100*(x(2) - (x(1))^2)^2 + (1 - x(1))^2;
I suspect I'm missing something very simple here, but what?
you need to pass a handle to the function #objFun not the function itself, and your A and x0 matrix need to be transposed, ie: a row with 2 columns, each row in A is another linear constraint.
x0=[1, -1];
A = [1,2];
b = 1;
[x, fval] = fmincon(#objFun, x0, A,b,[],[],[0; -inf],[inf; 0]);
function f=objFun(x)
f = 100*(x(2) - (x(1))^2)^2 + (1 - x(1))^2;
end

Passing the numeric matrix to symbolic function

I have created a function containing symbolic expressions. The expressions make use of a symbolic matrix. I want to solve the function passing the numeric matrix which replaces the symbolic matrix and provides a numeric answer. I can't seem to understand how to pass the numeric matrix.
function example
R= sym('R',[3 3])
r_b= R([1 2], [3])
R_bar= R([1 2], [1 2])
R_til=R([2 3],[2 3])
syms alpha_bar beta_bar
a_bar= [1;alpha_bar]
b_bar= [beta_bar;1]
P= sym(R_bar*a_bar)
Q= sym(R_til*b_bar)
syms E_a E_b
u_bar= [1; 0]
v_bar= [0;1]
W = sym(E_a*u_bar)
X= sym(E_b*v_bar)
C= sym(P==W)
D= sym(Q==X)
[alpha_bar_, E_a_] = solve(P==W,[alpha_bar,E_a])
[beta_bar_, E_b_] = solve(Q==X,[beta_bar,E_b])
a_bar= [1;alpha_bar_]
b_bar=[beta_bar_;1]
delta= (a_bar)'*r_b
gamma_b = delta/E_b_
gamma_a= delta/E_a_
a= [a_bar;0]-(gamma_b*[0;b_bar])
b= [0;b_bar]-gamma_a*[a_bar;0]
end
My R is R = [1 1 0; 1 3 2; 0 2 3].

Dot product of symbolic vector

I am trying to take the dot product of a symbolic vector and another vector. I did the following:
>> rac = sym('rac',[3 1])
rac =
rac1
rac2
rac3
>> i = [1;0;0]
i =
1
0
0
>> dot(rac,i)
ans =
conj(rac1)
However my desired outcome is rac1. Why is it not behaving like I want it to? And how do I achieve this output?
You need to specify that your symbolic vector is real:
rac = sym('rac', [3 1], 'real');
dot(rac, [1; 0; 0])
ans =
rac1

extended algorithm implementation in matlab

i have found following pseudo-code for extended euclidean algorithm
i implemented following algorithm
function [x1,y1,d1]=extend_eucledian(a,b)
if b==0
x1=1;
y1=0;
d1=a;
return;
end
[x1,y1,d1]=extend_eucledian(b,mod(a,b));
x1=y1;
y1=x1-floor(a/b)*y1;
d1=d1;
end
when i run following this program, i got following result
[x1,y1,d1]=extend_eucledian(23,20)
x1 =
0
y1 =
0
d1 =
1
i guess that [x1,y1,d1] are not changing their values during the iteration, for instance , i have tried following code
function [x1,y1,d1]=extend_eucledian(a,b)
if b==0
x1=1;
y1=0;
d1=a;
return;
else
x1=y1;
y1=x1-floor(a/b)*y1;
d1=d1;
end
[x1,y1,d1]=extend_eucledian(b,mod(a,b));
end
but i got
>> [x1,y1,d1]=extend_eucledian(23,20)
Undefined function or variable 'y1'.
Error in extend_eucledian (line 8)
x1=y1;
how can i fix this problem? where i am making mistake?
The problem can be solved by introducing intermediate working variables, that will store the result of the recursive call:
function [x,y,d]=extended_euclid(a,b)
if b==0
x=1;
y=0;
d=a;
return;
end
[x1,y1,d1]=extended_euclid(b,mod(a,b));
x=y1;
y=x1-floor(a/b)*y1;
d=d1;
end
This function works as expected:
>> [x, y, d] = extended_euclid(23,20)
x =
7
y =
-8
d =
1
>> [x, y, d] = extended_euclid(25,20)
x =
1
y =
-1
d =
5
The error in the question is that x1, y1, d1 are used simultaneously as working and output variables. The code can be rewritten using a new tern [x, y d] for the output values:
function [x y d]=eucledian_pairs(a,b)
%a>0,b>0
%d=gcd(a,b) a*x+y*b=d
[x1,y1,d1]=extend_eucledian(a,b);
x=y1;
y=x1-floor(a/b)*y1;
d=d1;
function [x1,y1,d1]=extend_eucledian(a,b)
if b==0
x1=1;
y1=0;
d1=a;
return;
end
[x1,y1,d1]=extend_eucledian(b,mod(a,b));
end
end
Executing this code gives the desired result.
>> [x y d]=eucledian_pairs(20,25)
x =
0
y =
1
d =
5

equationsToMatrix: how do I get the values of the variables?

When using equationsToMatrix you solve a set of linear equations as in the example (the solution is included)
syms x y z;
[A, b] = equationsToMatrix([x + y - 2*z == 0, x + y + z == 1, 2*y - z + 5 == 0], [x, y, z])
%solution of the equation set
A =
[ 1, 1, -2]
[ 1, 1, 1]
[ 0, 2, -1]
b =
0
1
-5
The vector b returns the values of the variables at issue: x,y, and z. However if I type x then MATLAB returns x and not 0, which is the solution of the equation in this case. This also occurs without adding the syms option.
The other problem is that if I type b(1) or b(2), I don't get any value: I would expect b to contain the values of x,y and z.
What I would need is to get something like this in the end
b(1) = 0
or
x = 0
What should I do to get the values of x,y,z by just typing x,y,z?
What you have is a way of converting symbolic linear equations into a numeric system by extracting the coefficient matrices. To solve the system you need to do
sol = A\b;
and now you can use the values in another expression with
subst(expr, {x,y,z}, {sol(1),sol(2),sol(3));
for example
A =
1 1 -2
1 1 1
0 2 -1
b =
0
1
-5
>> A\b
ans =
3.0000
-2.3333
0.3333