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)
Related
can anyone help me solve it? Or help me uncouple the equations? :(
$u_{xx}+u_{yy}=au+bw$
$w_{xx}+w_{yy}=cu+dw$
Also I have to solve this problem using the finite difference method, but once i get the output everything's zero. This is my Matlab code, if anyone could help me with that.
this is my code so far
clear; close all;
n=101; h=1/(n-1);
x=0:h:1.01; y=0:h:1.01;
U=zeros(n+1); W=zeros(n+1);
omega=1.7777777;err=1000;
a1=0.5; a2=5; b1=-1; b2=1;
tol=1e-6;
for i=1:n+1 %boundary conditions
U(1,i)=cos(5*pi*y(i)); U(i,1)=cos(5*pi*y(i)); U(n+1,i)=0; U(i,n+1)=0;
W(1,i)=cos(5*pi*y(i)); W(i,1)=cos(5*pi*y(i)); W(n+1,i)=0; W(i,n+1)=0;
end
figure(1);
subplot(1,2,1);mesh(x,y,U);view(130,25);grid on; title('boundary conditions U(x,y)');
subplot(1,2,2);mesh(x,y,W);view(130,15);grid on; title('boundary conditions W(x,y)');
u=U; w=W;
while err>tol
for i=2:n
for j=2:n
u(i,j)=(1-omega)*U(i,j)+omega*(((u(i+1,j)+u(i-1,j)+u(i,j+1)+u(i,j-1))/h^2)-b1*W(i,j))/(a1+(4/h^2));
w(i,j)=(1-omega)*W(i,j)+omega*(((w(i+1,j)+w(i-1,j)+w(i,j+1)+w(i,j-1))/h^2)-a2*U(i,j))/(b2+(4/h^2));
end
end
W=w; U=u; err=max(max(abs(u-U)));
end
figure(2);
subplot(1,2,1);mesh(x,y,U);view(130,25);grid on; title('U(x,y)');
subplot(1,2,2);mesh(x,y,W);view(130,15);grid on; title('W(x,y)');
The problem is this line:
W=w; U=u; err=max(max(abs(u-U)));
You are setting U=u`` first and then looking at the different of uandU```. Reverse the computation and it works.
err=max(max(abs(u-U))); W=w; U=u;
I have to solve the following system of nonlinear algebraic equations via fmincon. the problem is that fmincon should try to find the best case when my condition (x(N)-7.6<=Tol) can be satisfied until then fmincon needs to run again. so we need to use some loop to update the number of running fmincon and creating new initial condition either. but it seems there is no change of amount of N. how should I do?
function [x]=runnested(x0,N)
r=ones(4,1);
N=length(r);
Tol=0.001;
for i=1:N
x0=rand(5*N+13,1)
options = optimset('Largescale','off','algorithm','interior-point','Display','iter');
[x(i,:),fval,exitflag,output]=fmincon(#(x) norm(myfoctest(x)),x0,[],[],[],[],[],[],#myfoctest,options)
while x(N)-7.61>Tol
N=N+1;
end
end
function [C,Ceq]=myfoctest(x,N,r)
C=[];
r=ones(4,1);
N=length(r);
Ceq=zeros(5*N+13,1);
for j=1:N-1
Ceq(j)=x(3*N+1+j)-3*N+j)-2*x(4*N+1+j)*Ts*f*sin(x(2*N+1+j))./(pi*sin(i1)*x(j)^2)
Ceq(N+j)=x(4*N+1+j)-x(4*N+j)
Ceq(2*N+1+j)=x(3*N+1+j)*Ts*f*sin(x(2*N+1+j))+2*x(4*N+1+j)*Ts*f*cos(x(2*N+1+j))./(pi*x(j)*sin(i1))
Ceq(3*N+8+j)=x(j)-x(j+1)-Ts*f*cos(x(2*N+1+j))
Ceq(4*N+8+j)=Omeg0-x(j+1)+2*Ts*f*sin(x(2*N+1+j))./(pi*x(j)*sin(i1))
end
Ceq(N)=x(5*N+10)-x(5*N+9)-x(3*N+2)
Ceq(2*N)=x(5*N+12)-x(5*N+11)-x(4*N+2)
Ceq(2*N+1)=x(3*N+1)*Ts*f*sin(x(2*N+1))+2*x(4*N+1)*Ts*f*cos(x(2*N+1))/(pi*V0*sin(i1))
Ceq(3*N+1)=1-x(5*N+9)*b1-x(5*N+10)*b1-x(5*N+11)*b2-x(5*N+12)*b2-x(5*N+8)*N*Ts/100-x(5*N+13)
Ceq(3*N+2)=-2*x(5*N+8)*x(5*N+2)
Ceq(3*N+3)=-2*x(5*N+9)*x(5*N+3)
Ceq(3*N+4)=-2*x(5*N+10)*x(5*N+4)
Ceq(3*N+5)=-2*x(5*N+11)*x(5*N+5)
Ceq(3*N+6)=-2*x(5*N+12)*x(5*N+6)
Ceq(3*N+7)=2*x(5*N+13)*cos(x(5*N+7))*sin(x(5*N+7))
Ceq(3*N+8)=V0-x(1)-Ts*f*cos(x(2*N+1))
Ceq(4*N+8)=Omeg0-x(N+1)+2*Ts*f*sin(x(2*N+1))/(pi*V0*sin(i1))
Ceq(5*N+8)=-x(5*N+2)^2-N*Ts/100-N*Ts*x(3*N+1)/100
Ceq(5*N+9)=-x(5*N+3)^2-x(N)+a1+b1-b1*x(3*N+1)+7.61/100
Ceq(5*N+10)=-x(5*N+4)^2+x(N)+a1+b1-b1*x(3*N+1)-7.61/100
Ceq(5*N+11)=-x(5*N+5)^2-x(2*N)+a2+b2-b2*x(3*N+1)+0.35/100
Ceq(5*N+12)=-x(5*N+6)^2+x(2*N)+a2+b2-b2*x(3*N+1)-0.35/100
Ceq(5*N+13)=-(sin(x(5*N+7)))^2-x(5*N+1)
end
end
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
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!!! :)
Hi I want to run the code as below using PARFOR.
When I try it says that :
Valid indices for 'A_x' and 'A_y' are restricted in PARFOR loops.
Explanation For MATLAB to execute parfor loops efficiently, the
amount of data sent to the MATLAB workers must be minimal. One of the
ways MATLAB achieves this is by restricting the way variables can be
indexed in parfor iterations. The indicated variable is indexed in a
way that is incompatible with parfor. Suggested Action
Fix the indexing. For a description of the indexing restrictions, see “Sliced Variables” in the >Parallel Computing Toolbox documentation:
N=eveninteger;
H=zeros(N);
V=zeros(N);
A_x=zeros(N);
A_y=zeros(N);
parfor i=1:N;
for j=1:N;
if H(i,j)==-2;
t=0.3;
As_x=t*(j-i)/a;
As_y=t*(j-i)/a;
elseif H(i,j)==-3;
t=0.8;
As_x=t*(j-i)/(a*sqrt3);
As_y=t*(j-i)/(a*sqrt3);
elseif i==j
As_x=i;
As_y=i;
else
t=0;
As_x=0;
As_y=0;
end
for p=1:N/2
for q=N/2+1:N
A_x(p,q)=A_x(p,q)+As_x*(V(i,p)*V(j,q));
A_y(p,q)=A_y(p,q)+As_y*(V(i,p)*V(j,q));
end
end
end
end
I could not find solution. Could you offer me a solution.
Thanks in advance.
Erico
It looks like you're trying to perform a "reduction" on A_x and A_y using +. You might be able to work around this by doing something like the following:
parfor i = 1:N
A_x_tmp = zeros(N);
A_y_tmp = zeros(N);
for p=1:N/2
for q=N/2+1:N
A_x_tmp(p,q) = A_x_tmp(p,q) + ...
A_y_tmp(p,q) = A_y_tmp(p,q) + ...
end
end
A_x = A_x + A_x_tmp;
A_y = A_y + A_y_tmp;
end
In that way, PARFOR will understand the reduction operations on A_x and A_y.