How to write this equation that runs partitions? - matlab

I don't know where to go with this. I think I have the right stuff down but I don't understand.
https://imgur.com/a/V6gdDdr
It keeps running the loop forever and I don't know why.
n=9;
r =0;
p = 0;
syms x
v=1.7;
while abs(v-r) > 10^(-5)
n=n+1;
r = 0;
a = 0;
b= 1/n;
for i = 1:n
r = r + exp(((i+1)+exp(i))/2)*(b-a)
end
['done']
end
The output should be 85. But I'm getting like a couple thousand. I have tried changing the equation in the for loop but I do not know why it is giving me symbolic errors.

Several problems with the posted code:
The parentheses are misaligned in the r=r + ... statement
Instead of exp(i) and exp(i+1), you should use exp(bi) and exp(b(i+1)) in order to account for the given spacing.
Your value of "v" which is supposed be the exact answer should equal e-1, which is 1.71828. Using the approximate 1.7 is going to be very problematic when you are trying to converge to the exact solution within 5 decimal places.
Your for loop should go from 0 to n-1, this way you don't add up any values past your integral range.
n=9;
r =0;
p = 0;
v=e-1;
while abs(v-r) > 10^(-5)
n=n+1;
r = 0;
a = 0;
b= 1/n;
for i = 0:n-1
r = r + ((exp(b*(i+1))+exp(b*i))/2)*(b-a);
end
end

Related

MATLAB, fixed point iteration

I am given a function, f(x)= 5*sin(x)*exp(-x-0.2)-1, and I want to find a root using fixed point iteration. The instruction told me to re-arrange the equation to get x=g(x).
So I converted it to x=-ln[1/(5sin(x)]-0.2
I want to make a while loop that satisfies x(1+i)=g(i) relationship as I update x, until x converges. I am guessing that the 'converging' here means that abs(x(i)-x(i+1)) is very small, but not so sure.
x(1)=1;
while abs(x(i+1)-x(i)>0.0001 && i<50
g(i)=-log(1/(5*sin(x(i))))-0.2;
x(i+1)=g(i);
end
My code is obviously wrong. I know I have to update i like, i=i+1; somewhere, but I don't know how I should apply it. also, I see that I haven't declared x(1+1) in the beginning, and that's where I am getting an error from, but... no clue how to fix it.
please help.
If you want to do it in a while loop, then you need to do the first iteration outside the while loop:
x(1) = 1;
x(2) = -log(1/(5*sin(x(1)))) - 0.2;
i = 2;
max_iter = 50;
while (abs(x(i) - x(i-1)) > 0.0001) && (i < max_iter)
x(i+1) = -log(1/(5*sin(x(i)))) - 0.2;
i = i + 1;
end
Alternatively you could test for the number of elements of x in the while condition but that less elegant.
Put the test for convergence inside the loop since you have only one value of x to start with:
x(1) = 1;
max_iter = 50;
for i = 1 : max_iter
x(i+1) = -log(1/(5*sin(x(i)))) - 0.2;
if abs(x(i+1) - x(i)) < 0.0001
break; end
end

MATLAB - Finding Zero of Sum of Functions by Iteration

I am trying to sum a function and then attempting to find the root of said function. That is, for example, take:
Consider that I have a matrix,X, and vector,t, of values: X(2*n+1,n+1), t(n+1)
for j = 1:n+1
sum = 0;
for i = 1:2*j+1
f = #(g)exp[-exp[X(i,j)+g]*(t(j+1)-t(j))];
sum = sum + f;
end
fzero(sum,0)
end
That is,
I want to evaluate at
j = 1
f = #(g)exp[-exp[X(1,1)+g]*(t(j+1)-t(j))]
fzero(f,0)
j = 2
f = #(g)exp[-exp[X(1,2)+g]*(t(j+1)-t(j))] + exp[-exp[X(2,2)+g]*(t(j+1)-t(j))] + exp[-exp[X(3,2)+g]*(t(j+1)-t(j))]
fzero(f,0)
j = 3
etc...
However, I have no idea how to actually implement this in practice.
Any help is appreciated!
PS - I do not have the symbolic toolbox in Matlab.
I suggest making use of matlab's array operations:
zerovec = zeros(1,n+1); %preallocate
for k = 1:n+1
f = #(y) sum(exp(-exp(X(1:2*k+1,k)+y)*(t(k+1)-t(k))));
zerovec(k) = fzero(f,0);
end
However, note that the sum of exponentials will never be zero, unless the exponent is complex. Which fzero will never find, so the question is a bit of a moot point.
Another solution is to write a function:
function [ sum ] = func(j,g,t,X)
sum = 0;
for i = 0:2*j
f = exp(-exp(X(i+1,j+1)+g)*(t(j+3)-t(j+2)));
sum = sum + f;
end
end
Then loop your solver
for j=0:n
fun = #(g)func(j,g,t,X);
fzero(fun,0)
end

MATLAB linprog max iterations reached

I had written the following matlab code to optimize the following LP
max b'x
s.t A'x <= 0;
x <= d;
Also d is
d = {1,2..m}
and A is defined in the code. I am getting the error:
Maximum number of iterations exceeded; increase options.MaxIter.
Upon googling, someone said it is not very good that the error is occuring. and the problem has to reformulated. Any idea how to reformulate it.
The solution is very simple as A > 0, b> 0 and d>0 therefore x = 0
m = 10;
d = [1:1:m];
for j = 1:m,
for i = 1:m,
A(i,j) = 1/(i+j-1);
end
end
for i = 1:m,
b(i)=0;
end
for i = 1:m
lb(i) = -inf;
end
b;
lb = lb';
f = A*d';
[x,fval,exitflag,output] = linprog(-f,A,b,[],[],lb,d); %minimzation problem. Hence -f, A = A'
I'd used the optimist = ('MaxIter', 10000). To stop that error message.

index must be a positive integer or logical?

k = 0.019;
Pstar = 100;
H = 33;
h = 0.1;
X = 36;
N = round(X/h);
t = zeros(1,N+1);
P = zeros(1,N+1);
P(1) = 84;
t(1) = 0;
yHeun = zeros(1,N+1);
yHeun(1)=84;
a = 1; b = 100;
while b-a >0.5
c = (a+b)/2;
for n = 1:N
t(n+1) = t(n) + h;
Inside = nthroot(sin(2*pi*t/12),15);
Harvest = c*0.5*(Inside+1);
P(n+1) = P(n) + h*(k*P(n)*(Pstar-P(n))-Harvest(n));
if P < 0
P = 0;
end
yHeun(n+1) = yHeun(n) + h*0.5*((k*P(n)*(Pstar-P(n))-Harvest(n))+(k*P(n+1)*(Pstar-P(n+1))-Harvest(n+1)));
end
if sign(yHeun(c)) == sign(yHeun(a))
c = a;
else
c = b;
end
end
disp(['The root is between ' num2str(a) ' and ' num2str(b) '.'])
This is the code i'm trying to run and i know it probably sucks but im terrible at coding and every time i try to run the code, it says:
Attempted to access yHeun(50.5); index must be a positive integer or
logical.
Error in Matlab3Q4 (line 30)
if sign(yHeun(c)) == sign(yHeun(a))
I don't have ANY idea how to make yHeun(c or a or whatever) return anything that would be an integer. I dont think i did the while+for loop right either.
Question: "Begin with the higher bound of H being 100 (the high value results in a population of 0 after 36 months), and the lower bound being 1. Put the solver from Problem #3 above in the middle of a while loop and keep bisecting the higher and lower bounds of H until the difference between the higher and lower bound is less than 0.5."
I guess that line 30 (with the error) is this one:
if sign(yHeun(c)) == sign(yHeun(a))
Here, I guess c is equal to 50.5, as a result of c = (a+b)/2 above (BTW you can discover whether I guessed right by debugging - try adding disp(c) before line 30).
To force a number to be an integer, use floor:
c = floor((a+b)/2);
It seems you are trying to use some sort of divide-and-conquer algorithm; it should be enough to stop when b - a is equal to 1.

Counting Number of Specific Outputs of a Function

If I have a matrix and I want to apply a function to each row of the matrix. This function has three possible outputs, either x = 0, x = 1, or x > 0. There's a couple things I'm running into trouble with...
1) The cases that output x = 1 or x > 0 are different and I'm not sure how to differentiate between the two when writing my script.
2) My function isn't counting correctly? I think this might be a problem with how I have my loop set up?
This is what I've come up with. Logically, I feel like this should work (except for the hiccup w/ the first problem I've stated)
[m n] = size(matrix);
a = 0; b = 0; c = 0;
for i = 1 : m
x(i) = function(matrix(m,:));
if x > 0
a = a + 1;
end
if x == 0
b = b + 1;
end
if x == 1
c = c + 1;
end
end
First you probably have an error in line 4. It probably should be i instead of m.
x(i) = function(matrix(i,:));
You can calculate a, b and c out of the loop:
a = sum(x>0);
b = sum(x==0);
c = sum(x==1);
If you want to distinguish x==1 and x>0 then may be with sum(xor(x==1,x>0)).
Also you may have problem with precision error when comparing double values with 0 and 1.