I need to write above equation to my Matlab code but while writing it I got confused. M0=1.695, tetha1=41.31 degree, and gamma=1.4.
When I insert those numbers the result is M1=2.5476, but it should be around 1.51. Would you please write that code in Matlab?
I will insert my code also;
M1 = sqrt((2.4^2*M0^4*(sind(t1)^2)-4*(M0^2*(sind(t1)^2)-1)*(1.4*M0^2*(sind(t1)^2)+1))/((2.8*M0^2*(sind(t1)^2)-2.4)*(0.4*M0^2*(sind(t1)^2)+2)));
I rewrote you function in Matlab and for your given values (gamma=1.4, M0=1.695, tetha1=41.31) I am getting the same values. Here's the code:
function y = m1(g, M0, t1) % Do not edit this line.
numarator = ((g+1)^2)*(M0^4)*(sind(t1)^2) - 4*((M0^2)*(sind(t1)^2)-1)*(g*(M0^2)*(sind(t1)^2)+1)
denominator = (2*g*(M0^2)*(sind(t1)^2)-(g+1))*((g-1)*(M0^2)*(sind(t1)^2)+2)
y = sqrt(numarator/denominator)
end % Do not edit this line.
Calling the function with given values:
m1(1.4, 1.695, 41.31)
gives:
numarator =
17.9440
denominator =
2.7648
y =
2.5476
ans =
2.5476
Related
I am working on an assignment that requires me to use the trapz function in MATLAB in order to evaluate an integral. I believe I have written the code correctly, but the program returns answers that are wildly incorrect. I am attempting to find the integral of e^(-x^2) from 0 to 1.
x = linspace(0,1,2000);
y = zeros(1,2000);
for iCnt = 1:2000
y(iCnt) = e.^(-(x(iCnt)^2));
end
a = trapz(y);
disp(a);
This code currently returns
1.4929e+03
What am I doing incorrectly?
You need to just specify also the x values:
x = linspace(0,1,2000);
y = exp(-x.^2);
a = trapz(x,y)
a =
0.7468
More details:
First of all, in MATLAB you can use vectors to avoid for-loops for performing operation on arrays (vectors). So the whole four lines of code
y = zeros(1,2000);
for iCnt = 1:2000
y(iCnt) = exp(-(x(iCnt)^2));
end
will be translated to one line:
y = exp(-x.^2)
You defined x = linspace(0,1,2000) it means that you need to calculate the integral of the given function in range [0 1]. So there is a mistake in the way you calculate y which returns it to be in range [1 2000] and that is why you got the big number as the result.
In addition, in MATLAB you should use exp there is not function as e in MATLAB.
Also, if you plot the function in the range, you will see that the result makes sense because the whole page has an area of 1x1.
I have some code here which illustrates the nested nature of some integrals that I want to perform in matlab. When I run the following code I get the error
Error using .*
Matrix dimensions must agree.
Error in fun1/integrand (line 7)
f = x.^2.*t;
it seems that the way I have set it up now does not allow for vectorized output of the integral function. What can I do to make my code run? I have commented below what I want the functions to do.
My code is as follows:
%Main script:
z = linspace(0,10,10);
I = zeros(10,1);
for i = 1:10
I(i) = fun2(z(i));
end
%Function 1 in separate file:
function I = fun1(x)
I = integral(#integrand,-1,1);
function f = integrand(t)
f = x.^2.*t;
end
end
%Function 2 in separate file:
function I = fun2(z)
I = integral2(#integrand,-1,1,-1,1);
function f = integrand(x,y)
f = z.*fun1(x).*y; // here fun1 should return the value for all x that is swept through by integral2 during its call.
end
end
The problem with your code is you are using integral2 and integral1 in a combination. This way integral2 generates a grid for x and y and integral1 generates the t values. This way the t and x values don't match in size, and even if they would they are part of a different grid.
As already discussed, using ingetral3 is the right choice. Alternatively you could simply "fix" this introducing a loop, but this results in much slower code:
function I = fun1(x)
I=nan(size(x));
for ix=1:numel(x)
I(ix) = integral(#(t)integrand(x(ix),t),-1,1);
end
function f = integrand(x,t)
f = x.^2.*t;
end
end
I am trying to recreate the following equation in MATLAB
This equation calculates the equivalent inertia.
I have values for Ln, Ln-1 and In which are stored in a matrices and I am assuming that Ltotal is simply the Max value of Ln. Den is a matrix storing the values for the equation.
% Ln
for lp = 1: bars-1
ln_p(1,lp) = radius_pin(1) - radius_pin(lp+1);
ln_w(1,lp) = radius_wheel(1) - radius_wheel(lp+1);
end
% Ln-1
for lp = 1:bars-1
lnMinus_p(1,lp) = radius_pin(1) - radius_pin(lp);
lnMinus_w(1,lp) = radius_wheel(1) - radius_wheel(lp);
end
% L^3 - (Ln-1)^3
lCubed_p = ln_p.^3 - lnMinus_p.^3;
lCubed_w = ln_w.^3 - lnMinus_w.^3;
% In
In_p = aChor_p.^3/12;
In_w = aChor_w.^3/12;
% Denominator of IE equation (inertia)
den_p = lCubed_p./In_p;
den_w = lCubed_w./In_w;
I need a code which will find the Ieq values (they should be in a matrix the same size as the inputs)
If you have an array L which contains all L_n values, and similarly for a vector I with values I_n, then your equation can be implemented simply:
I_eq = L_total^3 / sum( diff(L.^3)./I(2:end) )
I'll also re-write your code, because you seem to be missing the point about MATLAB syntax -- it can be pretty close to mathematical notation. I'll leave out the subscripts (because it's the same calculation for both the pin and wheel anyway):
lCubed = (radius(1)-radius(2:end)).^3 - (radius(1)-radius(1:end-1)).^3;
In = 1/12 * aChor_p.^3;
den = sum(lCubed./In);
I've just started learning Matlab(a few days ago) and I have the following homework that I just don't know how to code it:
Write a script that creates a graphic using the positions of the roots of the polynomial function: p(z)=z^n-1 in the complex plan for various values for n-natural number(nth roots of unity)
so I am assuming the function you are using is p(z) = (z^n) - 1 where n is some integer value.
you can the find the roots of this equation by simply plugging into the roots function. The array passed to roots are the coefficients of the input function.
Example
f = 5x^2-2x-6 -> Coefficients are [5,-2,-6]
To get roots enter roots([5,-2,-6]). This will find all points at which x will cause the function to be equal to 0.
so in your case you would enter funcRoots = roots([1,zeros(1,n-1),-1]);
You can then plot these values however you want, but a simple plot like plot(funcRoots) would likely suffice.
To do in a loop use the following. Mind you, if there are multiple roots that are the same, there may be some overlap so you may not be able to see certain values.
minN = 1;
maxN = 10;
nVals = minN:maxN;
figure; hold on;
colors = hsv(numel(nVals));
legendLabels = cell(1,numel(nVals));
iter = 1;
markers = {'x','o','s','+','d','^','v'};
for n = nVals
funcRoots = roots([1,zeros(1,n-1),-1]);
plot(real(funcRoots),imag(funcRoots),...
markers{mod(iter,numel(markers))+1},...
'Color',colors(iter,:),'MarkerSize',10,'LineWidth',2)
legendLabels{iter} = [num2str(n),'-order'];
iter = iter+1;
end
hold off;
xlabel('Real Value')
ylabel('Imaginary Value')
title('Unity Roots')
axis([-1,1,-1,1])
legend(legendLabels)
How do I plot the value of Approximation - Answer as s varies in the code below? If you look at my code below, you can see the method I used (I put it in a separate file).
However, it does not show me a graph from 1 to 1000. Instead the graph is from 999 to 1001 and does not have any points on it.
for s = 1:1000
error = LaplaceTransform(s,5) - (antiderivative(1,s)-antiderivative(0,s));
end
plot(s,error);
title('Accuracy of Approximation');
xlabel('s');
ylabel('Approximation - Exact Answer');
The functions used:
function g = LaplaceTransform(s,N);
% define function parameters
a=0;
b=1;
h=(b-a)/N;
x = 0:h:1;
% define function
g = ff(x).*exp(-s*x);
% compute the exact answer of the integral
exact_answer=antiderivative(b,s)-antiderivative(a,s)
% compute the composite trapezoid sum
If=0;
for i=1:(N-1)
If=If+g(i).*h;
end;
If=If+g(1).*h/2+g(N).*h/2;
If
with
function fx=ff(x)
fx=x;
and
function fx=antiderivative(x,s);
fx= (-exp(-s*x)*(s*x+1))/(s^2);
Any help would be appreciated. Thanks.
The following
for s = 1:1000
error = LaplaceTransform(s,5) - (antiderivative(1,s)-antiderivative(0,s));
end
plot(s,error);
already has several issues. The two main ones are that error is getting overwritten at each iteration, as #Amro has pointed out, and that s, your loop variable, is a scalar.
Thus, you need to write
difference = zeros(1000,1); %# preassignment is good for you
for s = 1:1000
difference(s) = LaplaceTransform(s,5) - (antiderivative(1,s)-antiderivative(0,s));
end
plot(1:1000,difference);
There is another error in the LaplaceTransform function
function g = LaplaceTransform(s,N);
[...]
g = ff(x).*exp(-s*x); %# g is an array
[...]
If %# If is calculated, but not returned.
I assume you want to write
function If = LaplaceTransform(s,N);
instead, because otherwise, you try to assign the array g to the scalar difference(s).