I cant get the result of y - matlab

I have this questions but my result is different with the exact result what is the wrong?
this is what i tried.
x = 2;
z = 3;
y = x^2*x*z+12*x*z+((exp(x)/log(x*z)-log(x*z)*nthroot(x*z,2)))*nthroot(x*z,3);
result of y is 95.5185 not equal to -21.455

MATLAB Logarithm Conventions
Breaking up long equations into separate terms is useful for debugging. Here the case was the logarithmic functions that were used.
log → log10() (base-10 logarithm)
ln → log() (natural logarithm)
x = 2;
z = 3;
Term_1 = (x^2)*x*z;
Term_2 = 12*x*z;
Term_3 = (exp(x))/(log(x*z) - log10(x*z)*sqrt(x*z));
Term_4 = nthroot(x*z,3);
y = Term_1 + Term_2 + Term_3*Term_4;
y
Ran using MATLAB R2019b

Related

How to use matlab to quickly judge whether a function is convex?

For example, FX = x ^ 2 + sin (x)
Just for curiosity, I don't want to use the CVX toolbox to do this.
You can check this within some interval [a,b] by checking if the second derivative is nonnegative. For this you have to define a vector of x-values, find the numerical second derivative and check whether it is not too negative:
a = 0;
b = 1;
margin = 1e-5;
point_count = 100;
f=#(x) x.^2 + sin(x);
x = linspace(a, b, point_count)
is_convex = all(diff(x, 2) > -margin);
Since this is a numerical test, you need to adjust the parameter to the properties of the function, that is if the function does wild things on a small scale we might not be able to pick it up. E.g. with the parameters above the test will falsely report the function f=#(x)sin(99.5*2*pi*x-3) as convex.
clear
syms x real
syms f(x) d(x) d1(x)
f = x^2 + sin(x)
d = diff(f,x,2)==0
d1 = diff(f,x,2)
expSolution = solve(d, x)
if size(expSolution,1) == 0
if eval(subs(d1,x,0))>0
disp("condition 1- the graph is concave upward");
else
disp("condition 2 - the graph is concave download");
end
else
disp("condition 3 -- not certain")
end

Octave: Not all numerical integrations are created equal

I wrote a script to compare the answers between integrating an equation exactly, summing it, and using various built-in numerical integration functions.
Here is my program:
xmin = -2*pi;
xmax = -xmin;
dx = 0.01;
x = [xmin:dx:xmax];
f = sin(x);
fstar = conj(f);
y = fstar.*f; %for the sum
fy = #(x) y;
fsum1 = sum(y.*dx); %approximation by treating integral as a sum
%definite integral (specific to int(sin^2(x)))
fsum2 = 0.5.*(xmax - sin(xmax).*cos(xmax) - xmin + sin(xmin).*cos(xmin));
%numerical integrations:
fsum3 = quad(fy,xmin,xmax);
fsum4 = quadv(fy,xmin,xmax);
fsum5 = quadl(fy,xmin,xmax);
fsum6 = quadgk(fy,xmin,xmax);
fsum7 = quadcc(fy,xmin,xmax);
Everything runs fine until it hangs on fsum4. Well, I should say that fsum1 and fsum2 work. However, fsum3 = 7.53812e-031 (wrong) and fsum4 = a matrix the same size as x. When I plot fsum4 vs. x I get a (sin(x))^2 function with an amplitude of a little over 12.
I've gotten the quad integration function to work before, but I don't understand what I'm doing wrong here.

MATLAB style griddedInterpolant usage from Interpolations.jl

I'm trying to convert a MATLAB program of mine to Julia. A key feature of this program uses the griddedInterpolant function in MATLAB. I have found the Julia replacement (Interpolations.jl) and I have done a simple test in the 2-dimensional case to be sure that I understand how it works. This particular program uses 4-D arrays though, and I can't seem to figure out how to work the Interpolations.jl method beyond 2 dimensions.
A very simplified example of the MATLAB behavior. Note that this is a trivial example because V is flat, but you get the point. What my program would do is be changing the values of V, kp1gv, and kp2gv within a loop.
nK_1 = 50;
nK_2 = 50;
nKP_1 = 10;
nKP_2 = 10;
K1 = linspace(0,100,nK_1);
K2 = linspace(0,100,nK_2);
KP1 = linspace(0,100,nKP_1);
KP2 = linspace(0,100,nKP_1);
nX = 3;
nY = 3;
X = [0.9,1,1.1];
Y = [0.95,1,1.05];
[k1gv,k2gv,xgv,ygv] = ndgrid(K1,K2,X,Y); % creates grid vectors
V = ones(nK_1,nK_2,nX,nY); % value func to be interpolated
Fit = griddedInterpolant(k1gv,k2gv,xgv,ygv,V,'linear'); % fitted val fun
[kp1gv,kp2gv,xpgv,ypgv,kk1,kk2] = ndgrid(KP1,KP2,X,Y,K1,K2);
Fitted = Fit(kp1gv,kp2gv,xpgv,ypgv);
What I am looking for is to duplicate this in Julia, either exactly as it is in MATLAB or by rewriting it to be faster. Here is my attempt now:
nK_1 = 50
nK_2 = 50
nKP_1 = 10
nKP_2 = 10
K1 = linspace(0,100,nK_1)
K2 = linspace(0,100,nK_2)
KP1 = linspace(0,100,nKP_1)
KP2 = linspace(0,100,nKP_1)
nX = 3
nY = 3
X = [0.9,1,1.1]
Y = [0.95,1,1.05]
V = ones(nK_1,nK_2,nX,nY)
# using the more compact notation here, would be similar to
# griddedInterpolant({K1,K2,X,Y},V,'linear') -- no issues with this fit
Fit = interpolate((K1,K2,X,Y),V,Gridded(Linear()))
# this is how I'm getting ndgrid functionality
KP1gv = Float64[i for i in KP1, j in KP2, x in X, y in Y, a in K1, b in K2]
KP2gv = Float64[j for i in KP1, j in KP2, x in X, y in Y, a in K1, b in K2]
Xgv = Float64[x for i in KP1, j in KP2, x in X, y in Y, a in K1, b in K2]
Ygv = Float64[y for i in KP1, j in KP2, x in X, y in Y, a in K1, b in K2]
# what I want to work
Fitted = Fit[KP1gv,KP2gv,Xgv,Ygv]
I've read the documentation on Interpolations.jl, and I know this should be doable, I just can't seem to get it to work.
Rather than construct all those ndgrid coordinate arrays, just do this:
fitted = [Fit[i,j,k,l] for i in KP1, j in KP2, k in X, l in Y]
But a couple of tips:
Anything performance-critical (like the line above) should be placed in a function. This is absolutely crucial. See the performance tips page of the manual.
This seems not to be well-documented, but Gridded is designed for cases where you have a rectangular grid, but where the spacing may not be regular. In cases of regular spacing, you can do better with
itp = interpolate(V, Linear(), OnGrid())
sitp = scale(itp, K1, K2, x, y)
where x and y are also linspace objects.

Evaluating a symbolic summation in MATLAB

Following code throws out an error.
syms z positive;
syms n;
syms m;
N = 10;
Ms = 10;
Es = 1;
pd = 0.9;
pd_dash = 1-pd;
pf = 0.1;
pf_dash = 1-pf;
pr = 0.1;
qr = 1-pr;
p = 0.005
pi = pf_dash*p;
pb = pd_dash*p;
qi = 1-pi;
qb = 1-pb;
sm = symsum( z^((n+1)*Es), n, 0, N-1 );
temp_sum = symsum(z^((n+m+1)*Es)*qr^(n+m)*pr, m, 0, N-1);
z=1; %assume a value of z
x = eval(sm); %works fine
y = eval(temp_sum);
% Error:The expression to the left of the equals sign is not a valid target for an assignment.
Please suggest a way to resolve this.
The problem that I suspect is: the temp_sum comes out to be in piecewise(...) which eval is not capable of evaluating.
What you actually did:
Create a symbolic expression
Create a variable z which is unused
Call a undocumented function sym/eval
I assume you wanted to:
Create a symbolic expression
Substitute z with 1: temp_sum=subs(temp_sum,z,1)
get the result. Here I don't know what you really have because I don't know which variables are symbolic unknowns and which constants. Try simplify(temp_sum). If you substituted all unknowns it should return a number.

how do I integrate, a function with many arguments using matlab

If I'm to integrate a function
y = -((F+h)M^3(cosh(h*M)+M*beta*sinh(h*M)))/(h*M*cosh(h*M)+(-1+h*M^2*beta)*sinh(h*M))- (alpha*(M^2*(F+h)*(-1+2*h^2*M^2+ cosh(2*h*M)-2*h*M*sinh(2*h*M)))/(8*(h*M*cosh(h*M)+(-1+h*M^2*beta)*sinh(h*M))^2));
with respect to x, where
phi = 0.6;
x = 0.5;
M = 2;
theta = -1:0.5:1.5;
F = theta - 1;
h = 1 + phi*cos(2*pi*x);
alpha = 0.2;beta = 0.0;
I have written an Mfile
function r = parameterIntegrate(F,h,M,beta,alpha,theta,phi)
% defining a nested function that uses one variable
phi = 0.6;
x = 0.5;
r = quad(#testf,0,1 + phi*cos(2*pi*x));
% simpson's rule from 0 to h
function y = testf(x)
h = 1 + phi*cos(2*pi*x);
theta = -1:0.5:1.5;
F = theta - 1;
M = 2;
beta = 0;
alpha = 0;
y = -((F+h)*M^3*(cosh(h*M)+M*beta*sinh(h*M)))/(h*M*cosh(h*M)+(-1+h*M^2*beta)*sinh(h*M))- (alpha*(M^2*(F+h)*(-1+2*h^2*M^2+ cosh(2*h*M)-2*h*M*sinh(2*h*M)))/(8*(h*M*cosh(h*M)+(-1+h*M^2*beta)*sinh(h*M))^2));
end
end
and called the function by
tol = [1e-5 1e-3];
q = quad(#parameterIntegrate, 0, h,tol)
or
q = quad(#parameterIntegrate, 0,1 + phi*cos(2*pi*0.5),tol)
its not working its giving me
Error using ==> plus
Matrix dimensions must agree.
What your error message means is that for some line of code, there are 2 matrices, but the dimensions don't match, so it can't add them. What I suggest you do to solve this is as follows:
Figure out exactly which line of code is causing the problem.
If the line has large numbers of variables, simplify them some.
Remember that if there are any matrixs at all, and you don't want to do matrix multiplication/division, use the .*, ./, and .^.
I suspect that if you change your multiplies/divides with step 3, your problem will go away.