I am trying to solve a non-linear equation by using a while loop in Matlab but it's not giving me the results and give me an error in line that uses fzero function.
Why does this happens? How to solve the issue? Could someone help me with this?
Thanks in advance.
function z=optp(z,p)
z=3.067;
p=0;
while (z-p) > 0.8
syms a y Q p
p=z;
w=3;
C=1;
P=5;
b=1.2;
c=8;
B=0.5;
R=A^B*c*p^-b;
Q=(0.5-1/(p+5))*2400*((A^0.5)/(p^(6/5)));
x=Q;
J1=int(int(a*x*0.01,y,a*x/R,100),a,0,Q/x);
J2=(b*x^2*0.01/R)*int(a^2*1,a,0,Q/x);
J3=(P*R*b/p)*(int(int(y*0.01,y,a*x/R,100),a,0,Q/x));
J4=(b*R^2/x)*int(y^2*0.01,y,0,Q/R);
J5=(R*0.2)*(int(int(y*0.01,a,y*R/x,1),y,0,Q/R));
J=J1+J3+J4-J2-J5;
z=fzero(# (p) J,p)
end
end
Related
I am going calculate the eigenvalues follow program:
I want to calculate the matrix H , and achieve eigenvalues.
acc=1.44e-10;
a=1.732*acc;
t=-2.550;
x1=0;
y1=0;
z1=0;
x2=acc*cos(60);
y2=acc*sin(60);
z2=0;
sh=0;
for i=-1:1
for j=-1:1
for k=1:2
sh=sh+1;
xk=acc*cos(60)*(k-1);
yk=acc*sin(60)*(k-1);
zk=0;
xx(sh)=(i*a)+(j*a/2)+xk;
yy(sh)=(sqrt(3)/2)*a*j+yk;
zz(sh)=zk;
ki(sh)=k;
R1(sh)=i;
R2(sh)=j;
end
end
end
L0=sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2);
r1=0:0.02:1;
kx=(2*pi/(sqrt(3)*a))*(1-r1);
ky=(2*pi/(3*a))*(1-r1);
for ik=1:50
for i=1:2
for j=1:sh
Dis(i+8,j)=sqrt((xx(i)-xx(j))^2+(yy(i)-yy(j))^2+(zz(i)-zz(j))^2);
if abs(Dis-L0)<0.1
Rx=R1(sh)*a+R2(sh)*a/2;
Ry=R2(sh)*sqrt(3)/2*a;
H(ki(i+8),ki(j))=H(ki(i+8),ki(j))+t*exp(1i*(kx(ik)*Rx+ky(ik)*Ry));
end
end
end
end
But I always get this error:
Error in (line 44)
H(ki(i+8),ki(j))=H(ki(i+8),ki(j))+t*exp(1i*(kx(ik)*Rx+ky(ik)*Ry));
Undefined function 'H' for input arguments of type 'double'.
How to solve this error?
If you initialize H in the beginning, the code works.
H = zeros(2,2);
Notice:
I do not know the way you are calculating the eigenvalues. You should use the predefined function eig to check your solution. Another ways is to use SVD to calculate the eigenvectors and eigenvalues.
I have been going round and round in circles trying to get Matlab to solve the resonator circuit equation with a time varying input voltage. It works just fine as long as all the in arguments of the derivative functions are scalar values.
I have gone through others questions and found answers suggesting anonymous functions or using interpolation. When I try to implement these suggestions, I get a return error that tells me I do not have enough initial conditions to match the output of ode function or the matrices being concatenated do not match..
Here is my code:
%% Define Parameters
f0=1494.72e6;
Q=80;
R=1;
L=(Q*R)/(2*pi*f0);
C=1/((2*pi*f0)^2*L);
tp=71e-9;
N=2^16;
n=(0:N-1);
TT=1e-6;
h=TT/N;
t=n*h;
start_pulse=1;
end_pulse=round(tp/h);
V=zeros(size(t));
%% Create Voltage Pulse
for ii=1:length(n);
if ii>=start_pulse && ii<=end_pulse
V(ii)=sin(2*pi*f0*t(ii));
end
end
%%
tspan=[0 TT];
x0=[0 0];
sol=ode45(#ode,tspan,x0,[],V);
int=(0:h:TT);
sint=deval(sol,int);
plot(int,sint*C);
MY ode funtion is the following:
function [ dx ] = ode( t,x,V)
f0=1494.72e6;
Q=80;
R=1;
L=(Q*R)/(2*pi*f0);
C=1/((2*pi*f0)^2*L);
dx1 = x(2);
dx2 =((-x(1)./(L*C))-(R*x(2)./L)-(V./(L*C)));
dx = [dx1; dx2];
end
As you can see, L,C,and R all are scalar values. If I replace 'V' in dx2 with '1', the program runs just fine. I need to change V to be the matrix defined above.
Any help at all would be greatly appreciated!!! Thanks in advance!!! :)
I am relatively new to Matlab. At the moment I am solving an equation using vpasolve. Since that needs to loop over 80 variables, it is slow.
% input variables
% ffsample = Fourier transformed reference waveform
% ffref = Fourier transformed reference waveform
len=80;
n1=1;
n2=1.95;
alpha_grad=30;
beta_grad=14.858;
% constants
tp12=2.*n1.*cos(alpha)./(n2.*cos(alpha)+n1.*cos(beta));
tp21=2.*n2.*cos(beta)./(n1.*cos(beta)+n2.*cos(alpha));
%functions with unknown n3
syms n3
gamma= asin(n2.*sin(beta)./n3);
rp23 = (n3 .* cos(beta) - n2.*cos(gamma)) ./ (n3 .* cos(beta) + n2 .* cos(gamma));
t = cputime;
res_para=zeros(len,1);
%loop to solve for n3
digits(5):
for i=1:len
res_para(i)=vpasolve(ffsample(i)./ffref(i) == (tp12*tp21*rp23) ./ (tp12*tp21), n3);
end
e = cputime-t
%convert results to complex numbers
res_para=double(res_para);
res_para=squeeze(res_para);
res_para=res_para';
Now, I am trying to convert this into an equation which uses solve before entering the loop. As far as I understand it, I should use solve in conjunction with vpa? At the moment I am trying like the following code, but it wouldn't start:
res_p=solve(x == (tp12*tp21*rp23) ./ (tp12*tp21), n3);
t = cputime;
res_para=zeros(len,1);
for i=1:len
x=ffsample(i)./ffref(i);
res_para=vpa(res_p(x));
end
Any help would be greatly appreciated.
Update
Thank you for your replies. I changed the digits(5) position.
And yes, digits(5) is a very low value indeed however, it speeds teh above code up by approximately 3 times.
As I said, I am relatively new to matlab and thought that solving the equation symbolically first and then just insert the variables would greatly reduce the time, but maybe I am wrong here.
Considering I use vpasolve to solve a very simple equation numerically:
function [ xval ] = example( )
syms x
for y=1:10
xval(y) = vpasolve(y == 5.*x+6, x);
end
Wouldn't it be faster if I solve the function y=5x+6 symbolically for x first and then just include the msising values in the loop? Something like this:
function [ xval ] = example2( )
syms x
% symbolic solve for x (???)
% y=5x+6 ==> x=(y-6)/5
sol=solve( y == 5.*x+6, x)
for y=1:10
xval(y) = sol(y);
end
Obviously example2 does not work.
I've been trying to vectorize this function for matlab:
function [Q,R]=gramSchmidtMod(A)
n=size(A,1);
R=zeros(n);
for j=1:n
R(j,j)=norm(A(:,j));
Q(:,j)=A(:,j)/R(j,j);
for i=j+1:n
R(j,i)=Q(:,j)'*A(:,i);
A(:,i)=A(:,i)-Q(:,j)*R(j,i);
end
end
end
i tried:
j=1:n
R(j,j)=norm(A(:,j));
Q(:,j)=A(:,j)/R(j,j);
i=j+1:n
R(j,i)=Q(:,j)'*A(:,i);
A(:,i)=A(:,i)-Q(:,j)*R(j,i);
but that doesn't respect the same order as it would when using two for loops.
Can anyone help me out here ?
Why won't you just use
[Q,R]=qr(A)
I have an equation in Matlab according to X parameter . I want to find the amount of X for the random amounts of F(x) .
and I tried the code below . but It gives me two different results while my equation should have just one result .
even I tried the roots(f) instead of solve(f) but it gave me an error :
??? Undefined function or method 'isfinite' for input arguments of
type 'sym'.
anybody can help me in this ?
what should I do ?
even if I have a wrong idea about solving this problem please tell me .
Thank you
function betaDistribution_2(a,b)
syms x ;
y=inline((x^(a-1))*((1-x)^(b-1)));
beta=quad(y,0,1);
g=(1/beta)*(x^(a-1))*((1-x)^(b-1));
% I have this equation and I want to find the amount of x for the random
%amounts of p
p=int(g,x,0,x);
for i=0:50
fxi=rand(1);
f=p-fxi;
xi=solve(f);
result=eval(xi);
disp(result)
end
end
Try to filter your solutions.
a=1;
b=2;
% a threshold for imagery part
SMALL=1e-9;
syms x real;
y=inline((x^(a-1))*((1-x)^(b-1)));
beta=quad(y,0,1);
g=(1/beta)*(x^(a-1))*((1-x)^(b-1));
p=int(g,x,0,x);
% return true for physically meaningfull results
filter = #(xc) abs(imag(xc))<SMALL && real(xc)>0 && subs(g, x, xc) > 0 && subs(p, x, xc)>0;
for m=0:50
fxi=rand(1);
f=p-fxi;
xi=solve(f, x);
result=eval(xi);
idx = arrayfun (filter, result);
result = result(idx);
% make sure it is OK
assert(length(result)==1);
disp(result)
end