how to fix Matrix dimensions must agree - matlab

I am trying to plot Z0 in term of t
I get this error Matrix dimensions must agree.
I know that ‘T’ is a scalar, and ‘X’ and ‘Y’ are (51x26) matrices, and ‘t’ is a (1x501) vector.It is not possible to multiply them, because they are not conformable to matrix multiplication
I need a solution please I am new to MATLAB
sigma0=0;
el= 0.5;
L= 1 ;
h= 0.5 ;
a= 1;
N= 3;
g=10;
rho=1000;
Z0=0;
t=0:0.01:5;
x=0:0.02:el;
y=0:0.02:L;
[X,Y]=mesh grid (x,y);
sigma=0;
T=3*pi/4;
for n=0:N
for m=0:N
A=pi*((m/el)^(2)+(n/L)^(2))^(0.5);
B=(g*A+(sigma/rho)*A^3)*atan(A*h);
C=B^(0.5);
Z=a*cos(C.*T).*cos((m*pi/el).*X).*cos((n*pi/L).*Y);
Zs=Z0+Z;
Z0=Zs;
end
end
m=3;
n=4;
A=pi*((m/el)^(2)+(n/L)^(2))^(0.5);
B0=(g*A+(sigma0./rho)*A^3)*atan(A*h);
C0=(B0.^(0.5));
Z0=(a.*cos(C0.*T)).*cos((m*pi/el).*X).*(cos((n*pi/L).*Y));
figure
subplot(221)
plot(t,length(Z0));
xlabel(' temps s');
ylabel(' élévation z(x,y)y');
title(' sans tension superficielle');
legend('sigma0')
The result I expect to see a sinusoidal figure

I think all you need is replacing plot(t,length(Z0)); with plot(1:length(Z0), Z0);
I am not getting an error message, and I can't see where you try to multiple by t.
Execute clear all just in case...
Except that, you posted a syntax error [X,Y]=mesh grid (x,y); should be [X,Y]=meshgrid (x,y);.
Replace plot(t,length(Z0));
With: plot(1:length(Z0), Z0);
Here is the result:

Related

question about solving system of equations using symbolic math

I want to use symbolic symbol to solve a system of linear equation. So I prepare the following code.
A=[1,2;3,4];
% syms x
x=sym('x_%d',[2 1]);
eqn=A*x==[1;2];
result=solve(eqn,x)
Interestingly, it works, but when I read the variable result, it gives a 1X1 struct with x_1 and x_2 are 1X1 sym. But what I expect get should be 2 real values, why? Could someone explain it? Remark: do not want to use A^-1*[1;2] to obtain the answer.
If you set the output to single variable solve returns a structure
data type that contains all the solutions, to get each solution use
the dot. assignment, like result.x_1 or result.x_2
The code is as follows
A=[1,2;3,4];
% syms x
x=sym('x_%d',[2 1]);
eqn=A*x==[1;2];
result = solve(eqn,x);
result.x_1
% 0
result.x_2
% 1/2
If you want to have result as an array, use multiple output format, like
result(1) for the first variable, result(2) for the second variable
The code is as follows
A=[1,2;3,4];
% syms x
x=sym('x_%d',[2 1]);
eqn=A*x==[1;2];
[result(1), result(2)] = solve(eqn,x);
result
% result = [0 , 1/2]

Datatype conversion?

Given a Differential Equation in matrix form:
f = #(t,y) [(a*y(1) + b*y(2)); (c*y(1) + d*y(2))];
a=-2; b=-1; c=1; d=-4
Solutions to my differential equation problem are
x(t)= e^(-3t) (t+1)
y{t)= e^(-3t) *t
General Solution is
Y(t)= e^(-3t) (xo,yo)' + t * e^(-3t) (xo-yo,xo-yo)'
Initial condition is (xo,yo)=(1,0)=(e;g)
Matlab Code:
syms xSol ySol ran with and without this statement with same error msg
t=-1.1:0.1:2.1;
plot(t,xSol) error occurs here
hold on
plot(t,ySol)
hold off
Error message:
Error using plot
Data must be numeric, datetime, duration or an array convertible to double.
Further study would indicate that xSol and ySol must be numeric numbers.
I have at initial condition (1;0);
xSol(t) = exp(-3*t) + t*exp(-3*t)
ySol(t) = t*exp(-3*t)
I need to change xSol(t) and ySol(t) to xSol and ySol without (t).
How to fix? Correction must work with complex numbers.
Examples online use fplot. Is that a better function to use in this application?
I would need to define two functions for xSolv and ySolv. My code works for generic differential equation in two variables.
Example online is
f = #(x) sin(1/x);
Assume we want to plot that between 0.01 and 1:
lims = [.01 1];
fplot(f, lims, '.-')
I tried
xSolvpa = vpa(xSol)
ySolvpa = vpa(ySol)
fplot(xSolvpa,[-2.1 1.5])
hold on
fplot(t,ySolvpa,[-2.1 1.5])
but got the wrong plot. This is disconcerting. No error is given.
How to do?
MM
CHanging syntax corrects the error nicely and now the correct plot is plotted.
xSolvpa = vpa(xSol)
ySolvpa = vpa(ySol)
lims=[-1.1 1.5]
fplot(xSolvpa, lims)
hold on
fplot(ySolvpa, lims)

Incorrect value range shown after numeric integration

I'm trying to plot the probability of error for the following equation using MATLAB, I want to use the command trapz for the numerical integration, the problem is that I get a fine shape for the plot, but the values in the y-axis are wrong, the whole curve should be between 0 and 1.2 but it is between 0.492 and 0.5!! Can anyone just tell me what is wrong in my code, or just give me a hint? I really need help. Here is my formula that I need to plot (written using Maketex):
This is my code:
close all; clear;clc;
Nr=2;Ns=2;
lmda1=.3; lmda2=.3;
lmdas=.1; lmdar=.1;
z= 0.0001:1:40;
k1=2;k2=2;
kr=2.*Nr;ks=2.*Ns;
ax=0;
avg=0.0001:1:40;
em=1;
ch=2;
for alp=1-k1.*.5:ch
for beta=1-k2.*.5:ch
for eta=0:ch
for N=0:ch
for M=0:ch
for Q=0:ch
for id=0:eta
for jd=0:N
for A=0:N-jd
%
up=.25.*exp(-lmda1./2).*(lmda1./2).^(alp).*(lmda2.^2./(4)).^(beta./2).*exp(-lmda2./2).*(lmda1./(4.*em.*avg)).^eta.*(lmda2./(4.*em.*avg)).^N.*exp(-lmdas.*Ns.*.5).*.25.^(ks.*.25-.5).*exp(-lmdar.*Nr.*.5).*.25.^(kr.*.25-.5).*(Ns.*lmdas.*.25).^M.*(Nr.*lmdar.*.25).^Q;
cy=up.*(1./(factorial(eta).*factorial(N).*factorial(M).*factorial(Q).*gamma(eta+alp+1).*gamma(N+beta+1).*gamma(M+ks.*.5).*gamma(Q+kr.*.5)));
cj=cy.*(factorial(eta)./(factorial(id).*factorial(eta-id))).*(factorial(N)./(factorial(jd).*factorial(N-jd))).*gamma(M+id+jd+ks.*.5);
f1=(cj.*(factorial(N-jd)./(factorial(A).*factorial(N-jd-A))).*em.^A.*(((em+1).^(N-jd-A))).*gamma(kr.*.5+Q+A));
f2=f1.*(2.^(kr.*.5+Q+A)).*avg.^(eta+N);
ax=ax+f2;
end
end
end
end
end
end
end
end
end
q2=2;n2=2;N2=1;eta2=1;
fun2 = exp(-z.*avg.*(1+1.5./avg)).*z.^(eta2+N2-1./2).*(1./((1+z).^(q2).*(1./2+z).^(n2)));
out= trapz(z,fun2);
b=.5.*(1-ax.*(1./sqrt(pi)).*out.*avg.^(1./2));
plot(avg,b);grid;
There was a few wrong expressions in your code. Also I suspect you should evaluate the integral within the loop. What is more your integral meshgrid z seems too coarse. The following code gives me a 0~1.2 range for the second term in P(e)
% close all; clear;clc;
Nr=2;Ns=2;
lmda1=.3; lmda2=.3;
lmdas=.1; lmdar=.1;
z= .01:.01:40;
k1=2;k2=2;
kr=2.*Nr;ks=2.*Ns;
ax=0;
avg=z;
em=1;
ch=2;
for alp=1-k1*.5:ch
for beta=1-k2*.5:ch
for eta=0:ch
for N=0:ch
for M=0:ch
for Q=0:ch
for id=0:eta
for jd=0:N
for A=0:N-jd
%
up=.25.*exp(-lmda1./2).*(lmda1.^2./4).^(alp/2).*(lmda2.^2./(4)).^(beta./2).*exp(-lmda2./2).*(lmda1./(4.*em.*avg)).^eta.*(lmda2./(4.*em.*avg)).^N.*exp(-lmdas.*Ns.*.5).*.25.^(ks.*.25-.5).*exp(-lmdar.*Nr.*.5).*.25.^(kr.*.25-.5).*(Ns.*lmdas.*.25).^M.*(Nr.*lmdar.*.25).^Q;
cy=up./((factorial(eta).*factorial(N).*factorial(M).*factorial(Q).*gamma(eta+alp+1).*gamma(N+beta+1).*gamma(M+ks.*.5).*gamma(Q+kr.*.5)));
cj=cy.*(factorial(eta)./(factorial(id).*factorial(eta-id))).*(factorial(N)./(factorial(jd).*factorial(N-jd))).*gamma(M+id+jd+ks.*.5);
f1=(cj.*(factorial(N-jd)./(factorial(A).*factorial(N-jd-A))).*em.^A.*(((em+1).^(N-jd-A))).*gamma(kr.*.5+Q+A));
C=f1.*(2.^(kr.*.5+Q+A)).*avg.^(eta+N);
q2=Q;
n2=M+id+jd+ks/2;
N2=N;
eta2=eta;
fun2 = exp(-z.*avg.*(1+1.5./avg)).*z.^(eta2+N2-1./2).*(1./((1+z).^(q2).*(1./2+z).^(n2)));
itgrl= trapz(fun2)*.01*.01;
v = avg.^(eta2+N2+1./2);
ax=ax+v.*C.*itgrl;
end
end
end
end
end
end
end
end
end
b=.5-.5/pi^.5 *ax;
plot(avg,b);grid;
I don't know why I need to multiply dz twice but this gives me the correct value range. But I think it has something to do with the v vector values.
>> [min(.5/pi^.5 *ax),max(.5/pi^.5 *ax)]
ans =
0.0002 1.2241

Eigenvalues calculations in Matlab?

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.

Matrix dimensions must agree, Newton method

I need to find roots of a function using Newton method. I enter interval and accuracy from keyboard. Here is my code
disp('Newton method')
fx=#(g) 5*sin(g.^3-2*g.^2-1);
fx1=#(g) 5*g*(3*g-4)*cos(-g.^3+2*g.^2+1);
fx2=inline('-5*((4-6*g)*cos(-g.^3+2*g.^2+1)-(4*g-3*g.^2).^2*sin(-g.^3+2*g.^2+1))');
e=input ('Enter accuracy:');
a=input ('enter a:');
b=input ('enter b:');
x0=a:e:b;
y= 5*sin(x0.^3-2*x0.^2-1);
y2= -5*((4-6*x0)*cos(-x0.^3+2*x0.^2+1)-(4*x0-3*x0.^2).^2*sin(-x0.^3+2*x0.^2+1));
plot (x0,y),grid
xlabel('x'),ylabel('y')
fa=fx(a);
n=0;
if (fa*y2>0)
x1=a;
else
x1=b;
end;
while(abs(fx(x1))>e)
n=n+1;
x1=x1-(fx(x1))/(fx1(x1));
end;
disp(sprintf('Answer:%g',x1))
disp(sprintf('Number of iterations:%g',n))
When I compile, it says:
Error using *
Inner matrix dimensions must agree.
Error in Untitled3 (line 10)
y2= -5*((4-6*x0)*cos(-x0.^3+2*x0.^2+1)-(4*x0-3*x0.^2).^2*sin(-x0.^3+2*x0.^2+1));
You are multiplying two 1xn vectors, that is not possible. This multiplication causes the error:
y2= -5*((4-6*x0)*cos(-x0.^3+2*x0.^2+1)-(4*x0-3*x0.^2).^2*sin(-x0.^3+2*x0.^2+1));
^ ^
| |
Using element-wise multiplication .* might be the solution, but I don't know what you try to implement.