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.
Related
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:
I want to evaluate the double integral of my objective function (named myfunction, see below) using the build-in-function integral2.
I have tried to run this script;
f = #(r,theta) myfunction(r,theta);
integral2(f,0,2,0,2*pi);
where myfunction is the following function:
function fkt=myfunction(r,theta)
x=r.*cos(theta);
y=r.*sin(theta);
los(:,1)=x;
los(:,2)=y;
norm = (sum( sqrt( los(:,1).^2 + los(:,2).^2)));
fkt=norm*r;
end
I am making the integral in polar coordinates, that why fkt=norm*r.
Matlab gives me the following error message:
>> untitled2
Subscripted assignment dimension mismatch.
Error in myfunction (line 8)
los(:,1)=x;
I can't figure out, what the problem is.
There are two things that can be improved:
los is undefined
los(:,1) is a column while x is a row, so the assignment has to fail.
You can correct this by defining los and change your assignment. For instance:
los = NaN(numel(r), 2);
los(:,1) = x';
los(:,2) = y';
But, why do you need the variable los? Just remove it and the error will be gone:
function fkt=myfunction(r,theta)
x=r.*cos(theta);
y=r.*sin(theta);
norm = (sum( sqrt(x.^2 + y.^2)));
fkt=norm*r;
end
Best,
x is a matrix and you try to assign it to a column vector.
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 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
So I have the following two functions:
delta_t=T/N_time;
delta_x=1/N_space;
rho=delta_t/delta_x^2;
phi=zeros(N_space+1,N_time+1);
phi(:,1)=initial_condition((0:N_space)*delta_x);
for j=1:N_time;
for i=2:N_space;
phi(i,j+1)=rho*(phi(i-1,j)+phi(i+1,j))+(1-2*rho)*phi(i,j);
end;
end;
phi = phi(end,:);
end
When I run my second function, I get this error:
Error using ==> plot Vectors must be the same lengths.
I don't know why the sizes are different. They should be the same since the second function calls the first function. Any help would be much appreciated.
I think you have your phi transposed somehow. Your x_var is an 1 x (N_space + 1) vector, your phi is (N_space + 1) x (N_time + 1) and you return phi(end, :), which will be 1 x (N_time + 1). Did you mean to return phi(:, end)?