Calculating power with ^ - matlab

I am trying to model a graph as shown in the attached image. The equation for which I am modelling is also shown in the image.
My codings are,
sigmafu=1660;
phi=0.0:0.01:90;
e=2.7183; %I searched on internet to find e value of Euler number and I
%found this.
%Dont know whether MATLAB bydefault has value of e, like MATLAB has value
%pi.
pw= (-0.3)*phi*(180/180);
F=sigmafu*(0)* 2.7183^(pw);
plot (phi,F)
I am getting the following error by using the above codings:
Error using ^. Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead.
Error in myeqsetlin (line 126): F=sigmafu*(0)* 2.7183^(pw);
Can anyone help me correct the code? Also, does MATLAB have the default value of e (Euler's number), if so how can I use it?

You can get the value for e with exp(1). meaning e^1.
sigmafu = 1660;
phi = 0.0:0.01:90;
pw = -0.3*phi*pi/180;
F = sigmafu*exp(pw);
plot(phi,F)

Related

MATLAB: Using FZERO on a function which has a vector output

I am working on my thesis and running in some programming problems in Matlab. I am trying to implement the ''golden Bisection Method'' to speed up my code. To this end, I've consulted the build in function FZERO.
So I am determining the difference between two vectors which are both (1x20).
Difference = Clmax_dist-cl_vec;
Clmax_dist comes from a semi-empirical method and cl_vec comes from the excecution of an external AVL.exe file.
Essentially, this difference depends only on one single variable AOA because the Clmax_dist vector is a constant. Hence, I am constantly feeding a new AOA value to the AVL.exe to obtain a new cl_vec and compare this again to the constant Clmax_dist.
I am iterating this until one of the element in the vector becomes either zero or negative. My loop stops and reveals the final AOA. This is a time consuming method and I wanted to use FZERO to speed this up.
However, the FZERO documentation reveals that it only works on function which has a scalar as input. Hence, my question is: How can I use FZERO with a function which has a vector as an output. Or do i need to do something totally different?
I've tried the following:
[Difference] = obj.DATCOMSPANLOADING(AOA);
fun=#obj.DATCOMSPANLOADING;
AOA_init = [1 20];
AOA_root = fzero(fun,AOA_init,'iter');
this gave me the following error:
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 423)
while fb ~= 0 && a ~= b
Error in CleanCLmax/run (line 11)
AOA_root = fzero(fun,AOA_init,'iter');
Error in InitiatorController/moduleRunner (line 11)
ModuleHandle.run;
Error in InitiatorController/runModule (line 95)
obj.moduleRunner(ModuleHandle);
Error in RunSteps (line 7)
C.runModule('CleanCLmax');
The DATCOMSPANDLOADING function contains the following:
function [Difference] = DATCOMSPANLOADING(obj,AOA)
[Input]= obj.CLmaxInput; % Creates Input structure and airfoil list
obj.writeAirfoils(Input); % Creates airfoil coordinate files in AVL directory
[Clmax_dist,YClmax,Cla_mainsections] = obj.Clmax_spanwise(Input); % Creates spanwise section CLmax with ESDU method
[CLa] = obj.WingLiftCurveSlope(Input,Cla_mainsections); % Wing lift curve slope
[Yle_wing,cl_vec] = obj.AVLspanloading(Input,CLa,AOA); % Creates spanloading with AVL
Difference = Clmax_dist-cl_vec;
end
If I need to elaborate further, feel free to ask. And of course, Thank you very much.
fzero indeed only works on scalars. However, you can turn your criterion into a scalar: You are interested in AOA where any of the elements in the vector becomes zero, in which case you rewrite your objective function to return two output arguments: minDifference, which is min(Difference), and Difference. The first output, minDifference is the minimum of the difference, i.e. what fzero should try to optimize (from your question, I'm assuming all values start positive). The second output you'd use to inspect your difference vector in the end.

Cannot use operator ^ on a vector value

function[df] = getDiscountFactor(t,T,r,i)
y=getYearFraction(t,T);
a=r(i,1)
df=1/((1+a)^y);
end
This is my code, thing is that y cannot do the (1+a)^y operation because it says it's a vector, but it is only a vector value r(i,1). When I call it in command line it prints a as 0.03 and then it shows this error
a =
0.0300
Error using ^
Inputs must be a scalar and a square
matrix.
To compute elementwise POWER, use
POWER (.^) instead.
If i use .^ operator problem is solved but df gets into a vector and I need it to be a single number.
You've checked the first input to see that it's a scalar, so that presumably means the other input, y, is not a square matrix. If you check what y is too, you will probably see what's gone wrong.
And if not, does matlab have a function that tells you what type matlab thinks an object is? Run that on a and on y. Or to be extra pedantic, run that on 1+a. That should clear things up.

Calculating the expected value of a transformed random variable in MATLAB?

I am trying to compute the following expected value for Z being lognormally distributed
E[Z^eta w(F_Z (Z))^-eta]
where eta is a real number, F_Z the distribution function of Z and w:[0,1]->[0,1] an increasing function.
First of all, I am pretty new to Matlab so I don't know which way of integrating is the better one, numerically or symbolically. I tried symbolically.
My idea was to subsequently define functions:
syms x;
g_1(x) = x^eta;
g_2(x) = logncdf(x);
g_2(x) = w(x)^-eta;
g_4(x) = g_1(x) * g_3(g_2(x));
And then
exp = int(g_4(x),x,0,inf)
Unfortunately this doesn't work and MATLAB just posts the whole expression of g_4...
Is it better to use the numerical integration quadqk? What am I doing wrong here? I already read something about MATLAB not being the best program for integration but I have to use it so switching to a different program does not help.
Thanks a lot!

Using a value from a matrix as a multiplier in an equation?

Started using MatLab a couple of weeks ago, I don't know much proper syntax / terminology.
I'm trying to use a value in a 3x1 matrix as a multiplier in an equation later.
This is to draw a circle with radius and centre point defined by values input by the user.
I have a pop-up window, the values are input by the user and stored in this 3x1 cell (labelled answer).
How do I use the second value of that matrix, answer(2), in the following equation:
x = 'answer(2)' * cos(theta) + xCentre;
This error message appears:
Error using .*
Matrix dimensions must agree.
Error in Disks (line 40)
x = 'answer(2)'.* cos(theta) + xCentre;
In MATLAB, apostrophes ('') define a string. If the name of your matrix is answer, you can refer to its second value by the command answer(2) as mentioned by #Schorsch. For more infos on vectors and matrices, you can check this site.
In addition to what the previous answer says, its important to understand what exactly you are doing before you do it. Only add the ('') if you are defining a string, which generally occurs when dealing with variables. In your case, you simply have a matrix, which is not a string, but rather a set of numbers. You can simply do answer(2) as aforementioned, because answer(2) calls up the second value in your matrix while 'answer(2)' has you trying to define some variable that does not exist.
the most important thing is truly understanding what you are doing to avoid the basic syntax errors.

Matlab error while finding log

I am trying to find log of base 10 of each pixel of an image in matlab using following code
m1 = imread('owl','pgm');
for x = 1:size(m1,1)
for y = 1:size(m1,2)
m1(x,y) = log10(m1(x,y));
end
end
here m1 is a 2-D array of order 221 X 201.
but I am facing this error
??? Undefined function or method 'log2' for input arguments of type 'uint8'.
Error in ==> log10 at 20
y = log2(x);
Error in ==> q2 at 38
m1(x,y) = log10(m1(x,y));
but when I debug log function using following code
fprintf('log of 190 is %d', log10(190));
it gives me right output I dont know what happened when I use the same code in the loop.
The error message tells you what the problem is, you've tried to apply the log10 function to a value of type uint8 and the function is not defined for that type of number. What you haven't realised is that imread, when an image file meets certain criteria (read the documentation for what those criteria are) will capture the pixel data into an array of uint8s, not real numbers.
If you want to take the logarithm of a uint8 you'll either have to define a logarithm function of your own which takes such inputs, or, more straightforward, cast the uint8 to a type which log10 is happy with. For example, you could write:
log10(double(m1(x,y)))
And by now you'll have realised why your diagnostic test didn't tell you anything useful, when you execute the command log10(190) Matlab, by default, decides that 190 is of type double and computes the logarithm without complaint. log10(uint8(190)) tells a different story.