Angle between two lines in Maple - coordinates

I am trying to find the angle between two lines in Maple, but I keep getting this error.
`restart:
with(Student[MultivariateCalculus]):
a:=Line([0,0],[0,2]);
a := << Line 1 >>
b:=Line([0,0],[2,0]);
b := << Line 2 >>
Angle(x,y);
Error, invalid input: Student:-MultivariateCalculus:-Angle expects its 1st
argument, x, to be of type {Student:-MultivariateCalculus:-Line, Student:-
MultivariateCalculus:-Plane, Vector}, but received x`
I know that the answer to this angle is 90 degrees, I just gave a simple example so I can understand it.

Given that you assigned to a and b (rather than x and y) then try the command,
Angle(a, b);
For example (this is from an actual session),
restart;
with(Student[MultivariateCalculus]):
a:=Line([0,0],[0,2]);
a := << Line 1 >>
b:=Line([0,0],[2,0]);
b := << Line 2 >>
Angle(a,b);
1
- Pi
2
Angle(a,b) * 180/Pi;
90

Related

Simplify symbolic expression in matlab and get only the coeeficents

I'm looking for finding the coefficients from the Taylor's series in Matlab. The way that I'm doing is:
% Declare symbolic expression and function:
syms x;
f = exp(x);
% Calculate the taylor expansions in a concrete point:
T = taylor(f, x, 0.5);
% And finally I simplify the expression:
coefs = simplify(T)
But the returned expression is:
Yes, the expression it's simplified, but actually what I want is:
Where each term is multiplied by his coefficient. How can I this? Options suchs as simplify(f, x, 0.5, 10, where 10 refers to the simplification step,doesn't work in my case. Also I've been seeing this question and the problem is the same:
How get to simplify a symbolic and numeric mixed expression in Matlab
Depending on how many digits you want and in what exact format you want the result, here is an example:
>> c = double(coeffs(T))
c =
Columns 1 through 4
0.999966624859531 1.000395979357109 0.498051217190664 0.171741799031263
Columns 5 through 6
0.034348359806253 0.013739343922501
>> digits 15
>> x.^(0:numel(c)-1) * sym(c,'d').'
ans =
0.0137393439225011*x^5 + 0.0343483598062527*x^4 + 0.171741799031263*x^3 + 0.498051217190664*x^2 + 1.00039597935711*x + 0.999966624859531
EDIT
Note that you could also use vpa( ) instead of converting to double first:
>> c = coeffs(T)
c =
[ (2329*exp(1/2))/3840, (233*exp(1/2))/384, (29*exp(1/2))/96, (5*exp(1/2))/48, exp(1/2)/48, exp(1/2)/120]
>> x.^(0:numel(c)-1) * vpa(c).'
ans =
0.0137393439225011*x^5 + 0.0343483598062527*x^4 + 0.171741799031263*x^3 + 0.498051217190664*x^2 + 1.00039597935711*x + 0.999966624859531

Trying to find a transfer function from the discrete system below

I'm trying to solve the system below in Matlab. This system is a discrete system. I need to convert to a state space model system, to extract 4 matrices. Then find the transfer function.
y(k+2) + 4y(k+1) + 5y(k)= u(k+2)+2u(k+1)+u(k).
I solved this by hands and I found the four matrices:
A=[0,1:-5,-4]
B=[-2;4]
C=[1,0,0]
D=[1]
My problem is when I try to run my below code I got this error:
Error using ss2tf (line 26)
The A and C matrices must have the same number of columns.
Error in no1 (line 5)
[N1,D1]=ss2tf(A,B,C,D,1);
My Matlab code:
A=[0,1;-5,-4];
B=[-2;4];
C=[1,0,0];
D=[1];
[N1,D1]=ss2tf(A,B,C,D,1);
H=tf(N1,D1)
I expect to get a transfer function
Don't forget that you are dealing with a discrete-time system (add 1as third argument to ss2tf). If you correct the C matrix as already noticed in the comment, then the following code will do what you want:
A = [0,1;-5,-4];
B = [-2;4];
C = [1,0];
D = 1;
[N1,D1] = ss2tf(A,B,C,D);
H = tf(N1,D1,1)
H =
z^2 + 2 z + 1
-------------
z^2 + 4 z + 5

Matlab Error Expression or statement is incorrect--possibly unbalanced

Hello i get this error when i run the program I think it is from e (exponent)
the question is
Show that the function f(x) = e^x −(x^2/2) -x -1
has zero of multiplicity 3 at α = 0
and then, find the approximate solution of the zero of the function with the help
of the Newton’s method, first and second modified Newton’s methods, by taking
initial approximation x0 = 1.5 within an accuracy of 10^−4
.
function sol=newton(fn,dfn,x0,tol)
old=x0+1;
while abs(x0-old)>tol
old=x0;
x0=old-feval(fn,old)/feval(dfn,old)
end
sol=x0;
I typed this
fn = #(x) e^x -(x^2/2) -x -1
and the other inputs
i got the error on the title
Yes, it's the e. Replace the e with exp for the exponential operator.
fn = #(x) exp(x) -(x^2/2) -x -1;
FWIW, the derivative of the above function dfn is:
dfn = #(x) exp(x) - x - 1;

Reshape function output size is unknown

I am using Matlab coder to compile some .m files into C static library. In the function below, I am getting the following errors:
function net = mlpunpak(net, w)
nin = net.nin;
nhidden = net.nhidden;
nout = net.nout;
mark1 = nin*nhidden;
net.w1 = reshape(w(1:mark1), nin, nhidden); % Error1 ***
mark2 = mark1 + nhidden;
net.b1 = reshape(w(mark1 + 1: mark2), 1, nhidden); % Error2 ***
mark3 = mark2 + nhidden*nout;
net.w2 = reshape(w(mark2 + 1: mark3), nhidden, nout);% Error3 ***
mark4 = mark3 + nout;
net.b2 = reshape(w(mark3 + 1: mark4), 1, nout); % Error4 ***
Error1: Dimension 1 is fixed on the left-hand side but varies on the
right ([10 x 8] ~= [:? x :?]). Error2: Dimension 1 is fixed on the
left-hand side but varies on the right ([8 x 1] ~= [:? x :?]).
Error3: Dimension 1 is fixed on the left-hand side but varies on the
right ([8 x 1] ~= [:? x :?]). Error4: Dimension 2 is fixed on the
left-hand side but varies on the right ([1 x 1] ~= [1 x :?]).
The value of the variables are nin=10, nhidden=8, nout=1 and this function overwrites the fileds of the net. Any help is highly appreciated.
I think you gave the fields w1, b1, w2, b2 the fixed dimensions somewhere. In this case, you are using a variable-size array as input of reshape, that causes the problem. Have a look at this.
UPDATE: ok, i think i solved the errors. In the Overview tab of Matlab coder, i tried to define the fields as matrix of double with unbounded dimensions. And whoops, Code generation successful: View report :-)
Btw, at the Error 2, i think it's your fault, since the output of reshape here should be 1x8, you have to check it yourself regarding to your algorithm.

Prevent Matlab from rounding output?

Im running a simple script to estimate roots for a function. Everything works great, each iteration of the algorithm prints off the current x and f(x), but when the script finishes and sets the final estimate of x as the output of the function the value is returned and rounded to 3 decimal places...
while k < maxit
k = k + 1;
dx = b - a;
xm = a + 0.5*dx; % Minimize roundoff in computing the midpoint
fm = feval(fun, xm, diameter, roughness, reynolds);
fprintf('%4d %12.20e %12.4e\n',k,xm,fm);
if (abs(fm)/fref < feps) | (abs(dx)/xref < xeps) % True when root is found
r = xm;
return;
end
here is the tail bit of the output:
k xm fm
45 6.77444446476613980000e-003 1.3891e-012
46 6.77444446478035060000e-003 -1.3380e-011
47 6.77444446477324520000e-003 -5.9952e-012
48 6.77444446476969250000e-003 -2.3022e-012
49 6.77444446476791610000e-003 -4.5830e-013
ans =
0.0068
i dont know why its rounding the output.... how do i prevent that?
try typing 'format longE' in the command line before running the script
I had that problem too. Check out this page. It allows you to control the style of your outputs better.
http://www.mathworks.co.uk/help/techdoc/ref/format.html