troubles ploting double integral matlab - matlab

I want to plot the double integral
using MATLAB for s between 0 and 1. How can I do this?
I've tried the following code but since this is my first time using MATLAB it didn't worked after two hours:
syms r x s
fun=#(r,x)(r^8*sin(x)^8)/((r*sin(x)+1)^7)+....
f=int(fun,x,0,2*pi)
q=int(f,r,0,1)
Thanks for your help!

You can use function dblquad for each value of s on interval.For example(other parts add the feature yourself):
Int = zeros(1,101);
F = #(x,r) (r.^8.*(sin(x).^8)./(r.*sin(x)+1).^7);
for s = 1:101
Int(s) = dblquad(F,0,(s-1)*0.01,0,2*pi);
end
plot(0:0.01:1,Int)

Related

How can I insert my mathematical formula?

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

How to resolve MATLAB trapz function error?

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.

Beginner in matlab : syntax to recup value of a line matrice

i'm beginner in MATLAB. My problem is :
When i want to take the value of the line in my matrice L(1x2) to stock it in the a and b variables, I tried to use the following code
[a,b]=L;
It didn't work well. I don't understand why it's not possible like this. I used this syntax for the line ginput(n). I used another method to take the value but I want to understand my error in the code above.
My final code is this :
clf();
n=10;
axis([0 10 0 10]);
[px,py] = ginput(n);
Y = py';
X = ones(1,n);
X=[X ; px'];
L= Y*pinv(X);
a = L(1,1);
b = L(1,2);
x = 0:0.2:10;
plot(x,b*x+a, px,py,'r+');
grid;
Thanks a lot
You cannot assign in MATLAB using
[a,b]=L
Instead, you can do the following:
L=[1,2];
L=num2cell(L);
[a,b]=deal(L{:});
Then a=1 and b=2.

VHDL Code Generation using Matlab HDL Coder

I am sorry If I say some thing silly.Please forgive me:
I am trying to convert Matlab code(given below) to VHDL code,using HDL coder.It contains a function called sum.But when I try to convert the code it gives me error :
code generation only supports SumModes 'SpecifyPrecision' and
'KeepLSB' for 'SUM' when the size of the inputs can vary at run-time.
But the thing is I have never used functions before.Can any one please help me on it.How should change my code to convert it to VHDL.It would be really nice!
function y = fcn(n,y1,y2)
n=10;
x1c=zeros(2*n-1,1);
for i=1:2*n-1
if(i>n)
j1=1;
k1=2*n-i;
j2=i-n+1;
k2=n;
else
j1=n-i+1;
k1=n;
j2=1;
k2=i;
end
x1c(i)=sum((y1(j1:k1)).*y2(j2:k2));
end
x1c=flipud(x1c).';
y=x1c;
This is cross correlation of y1 and y2. y1 and y2 are two vectors of same length and n is length of y1. I am really stuck please help!
Thanks in advance!
#Haider: Take a look at
n=10;
y1 = 1:n;
y2 = n:-1:1;
x1c=zeros(1,2*n-1);
for i=1:2*n-1
y1_temp = zeros(1,2*n-1);
y2_temp = zeros(1,2*n-1);
if(i>n)
j1=1;
k1=2*n-i;
j2=i-n+1;
k2=n;
else
j1=n-i+1;
k1=n;
j2=1;
k2=i;
end
y1_temp(j1:k1) = y1(j1:k1);
y2_temp(j1:k1) = y2(j2:k2);
x1c(i)=sum(y1_temp.*y2_temp);
end
I compared the result with the Matlab xcorr function, and it seems the vector is reversed. Does this solve the error?

Sum of series in matlab using symsum

I have the following series
I tried this code but it does not print the final result...instead gives a long line of numbers!
syms n
y = symsum(1/sqrt(n),[1,100])
Result:
y =
2^(1/2)/2 + 3^(1/2)/3 + 5^(1/2)/5 + 6^(1/2)/6 + % and so on....
So the question is how to produce a final number as answer?!
Should I go with a script like this instead?
y = 0;
for i = 1:1:100
y = y + (1/sqrt(i));
end
disp(y);
To answer the original question, you can convert the symbolic expression you initially got using double, to convert from a symbolic to a numeric value:
y = double(y)
Or actually:
syms n
y = double(symsum(1/sqrt(n),[1,100]))
and you get 18.5896.
Additionally, you can use eval to evaluate the symbolic expression (thanks Luis Mendo).
Yay!
how about dropping the loop and use this instead:
n=1:100
result = sum(1./sqrt(n))
>> result =
18.5896
I'm not sure if you want to use the symbolic sum of series function in your case since you are only dealing with a simple function.