how can i convert from one value to multi values

how can i replace value of
alpha = 0.5
by multi values as
alpha =[0.1 0.3 0.5 0.6 0.66 0.9 1]
in the following code:
alpha = 0.5;
u0 = 0;
a_k = @(k) (k + 1)^(1 - alpha) - (k)^(1 - alpha);
n = 100;
a = 0;
b = 1;
h = (b - a) / n;
t = a:h:b;
f = @(t,u) (-u.^4) + (gamma(2*alpha+1) ./ gamma(alpha+1) ) .* (t.^alpha) - ...
(2./gamma(3 - alpha) ) .* (t.^(2 - alpha)) + (t.^(2*alpha) - t.^2).^4;
up = zeros(1, n);
uc = zeros(1, n);
zp = zeros(1, n);
uc = zeros(1, n); % ??? is this your "u"?
C = gamma(2 - alpha) * h ^ alpha;
for ni = 1:n
up(ni) = a_k(ni - 1) * u0;
for k = 1:ni - 1
up(ni) = up(ni) + (a_k(ni - 1 - k) - a_k(ni - k)) * uc(k);
end
zp(ni) = C * f(t(ni), up(ni));
uc(ni) = up(ni) + C * f(t(ni), up(ni) + zp(ni));
end
fprintf('%g\n', up(1:20))

 Respuesta aceptada

Image Analyst
Image Analyst el 20 de Jun. de 2022
Try this:
% Define all the alphas that we want to use.
allAlpha =[0.1 0.3 0.5 0.6 0.66 0.9 1]
% Iterate the code for each value of alpha.
for kk = 1 : length(allAlpha)
alpha = allAlpha(kk);
% Existing code below:
u0 = 0;
a_k = @(k) (k + 1)^(1 - alpha) - (k)^(1 - alpha);
n = 100;
a = 0;
b = 1;
h = (b - a) / n;
t = a:h:b;
f = @(t,u) (-u.^4) + (gamma(2*alpha+1) ./ gamma(alpha+1) ) .* (t.^alpha) - ...
(2./gamma(3 - alpha) ) .* (t.^(2 - alpha)) + (t.^(2*alpha) - t.^2).^4;
up = zeros(1, n);
uc = zeros(1, n);
zp = zeros(1, n);
uc = zeros(1, n); % ??? is this your "u"?
C = gamma(2 - alpha) * h ^ alpha;
for ni = 1:n
up(ni) = a_k(ni - 1) * u0;
for k = 1:ni - 1
up(ni) = up(ni) + (a_k(ni - 1 - k) - a_k(ni - k)) * uc(k);
end % of k loop
zp(ni) = C * f(t(ni), up(ni));
uc(ni) = up(ni) + C * f(t(ni), up(ni) + zp(ni));
end % of ni loop
fprintf('%g\n', up(1:20))
end % of kk loop

6 comentarios

work wolf
work wolf el 20 de Jun. de 2022
Editada: work wolf el 20 de Jun. de 2022
@Image Analyst Thank you so much. Please, how can i store every
up at allAlpha(kk) separated,
meaning
output=[ up(1:20) at (allallAlpha(1))' up(1:20) at (allAlpha(2))' up(1:20) at (allAlpha(3))' up(1:20) at (allAlpha(4))' ...
up(1:20) at (allAlpha(5))' up(1:20) at (allAlpha(6))' up(1:20) at (allAlpha(7))'];
This is useful for comparison between result.
Is that a matrix, row vector, or column vector? I don't understand what you put. Like (allAlpha(5))' is the same as allAlpha(5) since allAlpha(5) is just a scalar and there is no need to transpose it with '.
@Image Analyst i want result as matrix including every column at evey iteration,meaning up at alpha=0.1 then alpha=0.3 and so on as follows
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0.0497389463654563 0.0213889960049408 0.00577975952460106 0.00286678264632385 0.00185596757939561 0.000217889077015351 0
0.0756032841848556 0.0394787481302252 0.0123999751730029 0.00655408489750118 0.00438988350200604 0.000568453154565168 0
0.0937806925446395 0.0556290379398444 0.0193798935286129 0.0107113922486972 0.00734884592892711 0.00102120965764830 0
0.107860035746299 0.0704308289266767 0.0265305609236315 0.0151729898408803 0.0106037797699722 0.00155716732200875 0
0.119276626823768 0.0841960303939159 0.0337537642308159 0.0198414190139364 0.0140747131135280 0.00216271878416734 0
0.128782977112920 0.0971145894937620 0.0409904050975548 0.0246522821978120 0.0177066747337709 0.00282736971807349 0
0.136836519308874 0.109313451384330 0.0482014917128138 0.0295597555016475 0.0214592905356894 0.00354264272057228 0
0.143742977513616 0.120882992733044 0.0553596049729811 0.0345295051483806 0.0253015035929395 0.00430146626493324 0
0.149720693652571 0.131890546165088 0.0624445380331634 0.0395348080402283 0.0292085925801107 0.00509780051317483 0
0.154933371449271 0.142388021211973 0.0694408532510186 0.0445542468881334 0.0331603538578347 0.00592639232345276 0
0.159508321299395 0.152416520942504 0.0763364150931947 0.0495702537425901 0.0371399266969435 0.00678260656294221 0
0.163547351524805 0.162009299532333 0.0831214599873308 0.0545681439491122 0.0411329985829613 0.00766230529256888 0
0.167133636814165 0.171193743128752 0.0897879802538048 0.0595354498177205 0.0451272473771931 0.00856175844777340 0
0.170336250166931 0.179992745424877 0.0963293011057279 0.0644614459321516 0.0491119375908007 0.00947757605512695 0
0.173213269808024 0.188425691974366 0.102739781309983 0.0693368016829291 0.0530776205939369 0.0104066556516299 0
0.175813980719025 0.196509182520684 0.109014595844465 0.0741533209796437 0.0570159070743530 0.0113461407282561 0
0.178180480530615 0.204257572561355 0.115149574557707 0.0789037433441743 0.0609192910387687 0.0122933873534719 0
0.180348881531626 0.211683386918866 0.121141080069081 0.0835815892474123 0.0647810114200892 0.0132459369880677 0
You can just index the matrices with kk
zp(kk, ni) = C * f(t(ni), up(ni));
uc(kk, ni) = up(ni) + C * f(t(ni), up(ni) + zp(ni));
assuming zp and uc are row vectors in your original code. Using kk as the first argument would load them into the kk'th row of a matrix.
i did it, but give me all output, zores !!
% Define all the alphas that we want to use.
allAlpha =[0.1 0.3 0.5 0.6 0.66 0.9 1];
% Iterate the code for each value of alpha.
for kk = 1 : length(allAlpha)
alpha = allAlpha(kk);
% Existing code below:
u0 = 0;
a_k = @(k) (k + 1)^(1 - alpha) - (k)^(1 - alpha);
n = 100;
a = 0;
b = 1;
h = (b - a) / n;
t = a:h:b;
f = @(t,u) (-u.^4) + (gamma(2*alpha+1) ./ gamma(alpha+1) ) .* (t.^alpha) - ...
(2./gamma(3 - alpha) ) .* (t.^(2 - alpha)) + (t.^(2*alpha) - t.^2).^4;
up = zeros(kk, n+1);
uc = zeros(kk, n+1);
zp = zeros(kk, n+1);
% uc = zeros(kk, n+1); % ??? is this your "u"?
C = gamma(2 - alpha) * h ^ alpha;
for ni = 1:n+1 %length(t)
% up(ni) = a_k(ni - 1) * u0;
up(kk,ni) = a_k(ni - 1) * u0; %modfied
for k = 1:ni - 1
% up(ni) = up(ni) + (a_k(ni - 1 - k) - a_k(ni - k)) * uc(k);
up(kk,ni) = up(kk,ni) + (a_k(ni - 1 - k) - a_k(ni - k)) * uc(kk,k); % modfied
end % of k loop
% zp(ni) = C * f(t(ni), up(ni));
% uc(ni) = up(ni) + C * f(t(ni), up(ni) + zp(ni));
zp(kk, ni) = C * f(t(ni), up(kk,ni)); % modfied
uc(kk, ni) = up(kk,ni) + C * f(t(ni), up(kk,ni) + zp(kk,ni)); % modfied
end % of ni loop
% fprintf('%g\n', up(1:20))
end % of kk loop
up
gives,
up =
Columns 1 through 13
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 14 through 26
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 27 through 39
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 40 through 52
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 53 through 65
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 66 through 78
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 79 through 91
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 92 through 101
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
@Image Analyst thank you so much again. it's done with edite
up = zeros(kk, n+1);
uc = zeros(kk, n+1);
zp = zeros(kk, n+1);
before loop (for kk = 1 : length(allAlpha) ) and replace kk by length(allAlpha).
Best regards

Iniciar sesión para comentar.

Más respuestas (1)

Ayush Kumar Jaiswal
Ayush Kumar Jaiswal el 19 de Jun. de 2022
Editada: Ayush Kumar Jaiswal el 19 de Jun. de 2022
You want to calculate that function at different values of alpha, it can done using
arrayfun (func, arr);

Categorías

Más información sobre Modify Image Colors en Centro de ayuda y File Exchange.

Preguntada:

el 19 de Jun. de 2022

Comentada:

el 23 de Jun. de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by