Transfer function keeps coming up as empty - matlab

clear all
close all
clc
format short eng
R1=100;
R2=100;
R3=500;
R4=100;
R5=50;
C1=1e-6;
C2=1e-6;
C3=2e-6;
syms s Vin Va Vb Vc Vd Ve Vf C1 C2 C3 R1 R2 R3 R4 R5 Vout
Zc1=1/(s*C1);
Zc2=1/(s*C2);
Zc3=1/(s*C3);
e(1)=Vin==Va; %KVL Op Amp
e(2)=Ve==Vf;
e(4)=((Va-Vb)/Zc1)-((Vb-Vd)/Zc2)-(Vb/R5)==0; % KCL Node B
e(5)=((Va-Vc)/R1)-((Vc-Vd)/R2)-((Vc-Ve)/Zc3)==0; %KCL Node C
e(6)=((Vb-Vd)/Zc2)-((Vc-Vd)/R2)==0; %KCL Node D
e(7)=((Vc-Ve)/Zc3)-((Ve-Vf)/R3)==0; %KCL Node E
e(8)=((Ve-Vf)/R3)-(Vf/R4)==0; %KCL Node F
sol=solve(e,Va,Vb,Vc,Vd,Ve,Vf,Vout); %Sol
Vout=sol.Vf/R5+R4;
Vout=eval(sol.Vout)
H=Vout/Vin % Gain is output over input

You can't define resistors R1 R2 R3 R4 R5 as symbols when you already have their values, they got overwritten. Same for capacitors, C2 C3, and give some value for Vin in order to get value for Vout. Also, don't put the equations as a vector against variables, Vout is not a variable. just write the equations individually and leave the variables for MATLAB to determine automatically. Finally check the formula for Vout = Vf/R5 + R4, is it accurate?
clear, clc
R1 = 100; R2 = 100; R3 = 500; R4 = 100; R5 = 50;
C1 = 1e-6; C2 = 1e-6; C3 = 2e-6;
Vin = 5; % Give it some value to get Vout
syms s Va Vb Vc Vd Ve Vf
Zc1=1/(s*C1);
Zc2=1/(s*C2);
Zc3=1/(s*C3);
e1 = Vin == Va; % KVL Op Amp
e2 = Ve == Vf;
e4 = (Va-Vb)/Zc1 - (Vb-Vd)/Zc2 - Vb/R5 == 0; % KCL Node B
e5 = (Va-Vc)/R1 - (Vc-Vd)/R2 - (Vc-Ve)/Zc3 == 0; % KCL Node C
e6 = (Vb-Vd)/Zc2 - (Vc-Vd)/R2 == 0; % KCL Node D
e7 = (Vc-Ve)/Zc3 - (Ve-Vf)/R3 == 0; % KCL Node E
e8 = (Ve-Vf)/R3 - Vf/R4 == 0; % KCL Node F
sol = solve(e1,e2,e4,e5,e6,e7,e8); %Sol
Vout = sol.Vf/R5 + R4;
H = Vout/Vin % Gain is output over input
Output:
H =
20
20
20

Related

Why do i get "Error using vertcat" in Matlab ode45 solver?

I have the following function to be solved by ODE45 solver of Matlab:
function f = odefun(t, y)
global mu ft
% y = [a, h, k, p, q, L, lla, lh, lk, lp, lq, lL];
a = y(1);
h = y(2);
k = y(3);
p = y(4);
q = y(5);
L = y(6);
lla = y(7);
lh = y(8);
lk = y(9);
lp = y(10);
lq = y(11);
lL = y(12);
n = sqrt(mu./a.^3); % mean motion
G = sqrt(1-h.^2-k.^2);
K = 1+p.^2+q.^2;
sL = sin(L);
cL = cos(L);
r = (a.*G.^2)./(1+h.*sL+k.*cL);
% components of B Matrix
B11 = 2.*(n.^-1).*(G.^-1).*(k.*sL-h.*cL);
B12 = 2.*(n.^-1).*a.*(r.^-1).*G;
B13 = 0;
B21 = -(n.^-1).*(a.^-1).*G.*cL;
B22 = (n.^-1).*(a^-2).*r.*(G.^-1).*(h+sL)+(n.^-1).*(a.^-1).*G.*sL;
B23 = -(n.^-1).*(a.^-2).*r.*(G.^-1).*k.*(p.*cL-q.*sL);
B31 = (n.^-1).*(a.^-1).*G.*sL;
B32 = (n.^-1).*(a.^-2).*r.*(G.^-1).*(k+cL)+(n.^-1).*(a.^-1).*G.*cL;
B33 = (n.^-1).*(a.^-2).*r.*(G.^-1).*h.*(p.*cL-q.*sL);
B41 = 0;
B42 = 0;
B43 = 0.5.*(n.^-1).*(a.^-2).*r.*(G.^-1).*K.*sL;
B51 = 0;
B52 = 0;
B53 = 0.5.*(n.^-1).*(a.^-2).*r.*(G.^-1).*K.*cL;
B61 = 0;
B62 = 0;
B63 = n.*(a.^2).*(r.^-2).*G+(n.^-1).*(a.^-2).*r.*(G.^-1).*(q.*sL-p.*cL);
B = [B11 B12 B13;
B21 B22 B23;
B31 B32 B33;
B41 B42 B43;
B51 B52 B53;
B61 B62 B63];
% costate vector
lambdaVec = [lla lh lk lp lq lL];
% control law vector
uvec = lambdaVec * B;
unorm = norm(uvec);
u = uvec./unorm;
u = u';
z_ = (B*u).*ft;
C = [0; 0; 0; 0; 0; n.*(a.^2)*(r.^-2).*G];
zdot = z_+C;
which should solve a system of 12 ODEs with 12 initial conditions, however when i run the code i get the following error:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in odefun (line 49)
B = [B11 B12 B13;
which refers to matrix B. what possibly goes wrong here?
B11, B12, ... are the components of the matrix which are defined based on function variables e.g. "y" vector.

artificial neural network in octave

I'm having trouble on an easy exercise about an artificial neural network with 2 features, a hidden layer of 5 neurons and two possible outputs (0 or 1).
My X matrix is a 51x2 matrix, and y is a 51x1 vector.
I know I'm not supposed to do the while E>1 but I wanted to see if eventually my error would be lower than 1
I'd like to know what I am doing wrong. My error doesn't seem to lower (around 1.5 no matter how much iterations I'm doing). Do you see in the code where I am doing a mistake? I'm supposed to use gradient descent.
function [E, v,w] = costFunction(X, y,alpha1,alpha2)
[m n] = size(X);
E = 1;
v = 2*rand(5,3)-1;
w = 2*rand(2,6)-1;
grad_v=zeros(size(v));
grad_w=zeros(size(w));
K = 2;
E = 2;
while E> 1
a1 = [ones(m,1) X];
z2 = a1 * v';
a2 = sigmoid(z2);
a2 = [ones(size(a2,1),1),a2];
z3 = a2 * w';
h = sigmoid(z3);
cost = sum((-y.*log(h)) - ((1-y).*log(1-h)),2);
E = (1/m)*sum(cost);
Delta1=0;
Delta2=0;
for t = 1:m
a1 = [1;X(t,:)'];
z2 = v * a1;
a2 = sigmoid(z2);
a2 = [1;a2];
z3 = w * a2;
a3 = sigmoid(z3);
d3 = a3 - y(t,:)';
d2 = (w(:,2:end)'*d3).*sigmoidGradient(z2);
Delta2 += (d3*a2');
Delta1 += (d2*a1');
end
grad_v = (1/m) * Delta1;
grad_w = (1/m) * Delta2;
v -= alpha1 * grad_v;
w -= alpha2 * grad_w;
end
end

Matlab - Legend does not show different colors

can anybody help me?I cant show the legend lines different colors.How can I do it?
a1 = 0; b1 = 4;
a2 = 4; b2 = 10;
a3 = 6; b3 = 20;
x1=a1:.01:b1;
x2=a2:.01:b2;
x3=a3:.01:b3;
f1 = 1 ./ (b1 - a1);
f2 = 1 ./ (b2 - a2);
f3 = 1 ./ (b3 - a3);
plot(x1,f1,'r',x2,f2,'b',x3,f3,'y');
grid
xlabel('0 < x < 7 , 0.01 örnek aralığında') % x ekseni başlığı
ylabel('Üstel dağılımın olasılık yoğunluk fonksiyonu') % y ekseni başlığı
legend('s1','s2','s3')
You are plotting a vector x1, x2, x3 against a scalar f1, f2, f3. From the documentation for plot():
If one of X or Y is a scalar and the other is a vector, then the plot
function plots the vector as discrete points at the scalar value.
Each data point in your vector is plotted against the corresponding f value as a separate lineseries, giving you 2403 separate line series. In your legend call you add legend strings for the first 3 line series, which are all going to be red since the first 401 lineseries are red. If your desire is to plot a horizontal line you can create vectors from your f variables using repmat()
Using your example:
a1 = 0; b1 = 4;
a2 = 4; b2 = 10;
a3 = 6; b3 = 20;
x1=a1:.01:b1;
x2=a2:.01:b2;
x3=a3:.01:b3;
f1 = repmat((1 ./ (b1 - a1)), size(x1));
f2 = repmat((1 ./ (b2 - a2)), size(x2));
f3 = repmat((1 ./ (b3 - a3)), size(x3));
plot(x1,f1,'r',x2,f2,'b',x3,f3,'y');
grid
xlabel('0 < x < 7 , 0.01 örnek aralığında') % x ekseni başlığı
ylabel('Üstel dağılımın olasılık yoğunluk fonksiyonu') % y ekseni başlığı
legend('s1','s2','s3')

matlab: large system of coupled nonlinear equations

I'm trying to solve the a very large system of coupled nonlinear equations. Following this thread and the related help by Matalb (first example) I tried to wrote the following code:
%% FSOLVE TEST #2
clc; clear; close all
%%
global a0 a1 a2 a3 a4 h0 TM JA JB
a0 = 2.0377638272727268;
a1 = -7.105521894545453;
a2 = 9.234000147272726;
a3 = -5.302489919999999;
a4 = 1.1362478399999998;
h0 = 45.5;
TM = 0.00592256;
JA = 1.0253896074561006;
JB = 1.3079437258774012;
%%
global N
N = 5;
XA = 0;
XB = 15;
dX = (XB-XA)/(N-1);
XX = XA:dX:XB;
y0 = JA:(JB-JA)/(N-1):JB;
plot(XX,y0,'o')
[x,fval] = fsolve(#nlsys,y0);
where the function nlsys is as follows:
function S = nlsys(x)
global a1 a2 a3 a4 N TM h0 dX JA JB
H = h0^2/12;
e = cell(N,1);
for i = 2:N-1
D1 = (x(i+1) - x(i-1))./2./dX;
D2 = (x(i+1) + x(i-1) - 2.*x(i))./(dX^2);
f = a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3;
g = - H.* (a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3)./(x(i).^5);
b = (H/2) .* (5*a1 + 8*a2.*x(i) + 9*a3.*x(i).^2 + 8*a4.*x(i).^3)./(x(i).^6);
e{i} = #(x) f + b.*(D1.^2) + g.*D2 - TM;
end
e{1} = #(x) x(1) - JA;
e{N} = #(x) x(N) - JB;
S = #(x) cellfun(#(E) E(x), e);
When I run the program, Matlab gives the following errors:
Error using fsolve (line 280)
FSOLVE requires all values returned by user functions to be of data type double.
Error in fsolve_test2 (line 32)
[x,fval] = fsolve(#nlsys,y0);
Where are my mistakes?
Thanks in advance.
Petrus

Matlab - ??? Index exceeds matrix dimensions [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have the following code (part of a larger program) in matlab:
while(k<n)
C.(sprintf('Cnew')) = C.(sprintf('C%d',k));
d1 = equalize_dimension(a.F, C.(sprintf('Cnew')));
distance_new = distance(d1.x, d1.y);
k = k + 1;
end
If you want to substitute values since I have included part of the program, this would be as follows:
C.(sprintf('Cnew')):
78
And, for a.F it is as follows:
78 82 84 80
80 84 86 82
82 87 88 85
82 87 89 86
For the equalize_dimension(x,y) function, it is as follows:
function n = equalize_dimension (x,y)
[r1 c1] = size(x);
[r2 c2] = size(y);
if r1<r2
e= r2-r1;
for i=1:e
x(r1+1,1)=0;
r1 = r1 + 1;
end
[r1 c1] = size(x);
n.x =x;
n.y = y;
end
if r1>r2
e = r1-r2;
for i=1:e
y(r2+1,1)=0;
r2 = r2 + 1;
end
[r2 c2] = size(y);
n.x = x;
n.y = y;
end
if c1<c2
e= c2-c1;
for i=1:e
x(1,c1+1)=0;
c1 = c1 + 1;
end
[r1 c1] = size(x);
n.x = x;
n.y = y;
end
if c1>c2
e = c1-c2;
for i=1:e
y(1,c2+1)=0;
c2 = c2 + 1;
end
[r2 c2] = size(y);
n.x = x;
n.y = y;
end
if r1==r2 && c1==c2
n.x = x;
n.y = y;
end
And, for the distance(x,y) function, it is as follows:
function m = distance(x,y)
[r c] = size(x);
for i=1:r
for j=1:c
summation = (sum(sum(pdist2(x,y))));
end
end
m=summation;
end
When I run the program, I get the following error:
??? Index exceeds matrix dimensions.
Error in ==> fs at 36
distance_new = distance(d1.x, d1.y);
Why is that?
Thanks.
First of all, stop in debugger before line distance_new = distance(d1.x, d1.y);
and type
>> which distance
I suspect that you'll get as output
distance is a variable.
Meaning that you override the function distance by using a variable with the same name.
Second, in function distance what is the reason for the nested loop on i and j? You are not using these variables and summation is computed regardless of the nested loops.
please also try not to use i and j as variables.