linear interpolation of function handle

Hi everybody, I need to calculate the value in the range pi <= wave_angle < pi/2 of the a function handle @(freq_wave,wave_angle) which is defined as:
((log(B/T))^-1).*(0.87/CB).^((1+Fn).*cos(wave_angle)).*(1/3-(2/3).*cos(wave_angle))
in the range ((wave_angle >=0) & (wave_angle <= pi/2))
((log(B/T))^-1).*(0.87/CB).^(1+Fn) in the range (wave_angle=pi)&(vel_nave>V_G(freq_wave)./2)&(Fn_rel(freq_wave)>=0.12)
((log(B/T))^-1).*(0.87/CB) in the range (wave_angle=pi)&(vel_nave<V_G(freq_wave)./2)+ ((log(B/T))^-1).*(0.87/CB) .*(wave_angle=pi)&(Fn_rel(freq_wave)<0.12);
but I don't konw which function is the best to use. Thanks in advance for your help.

7 Commenti

Since you have a continuous formula for the function, there is nothing to interpolate. Just evaluate it at the locations you want.
Hi Matt, perhaps I explained badly the problem. I will show you the code
a1=@(freq_wave,wave_angle) ((log(B/T))^-1).*(0.87/CB).^((1+Fn).*cos(wave_angle)).*(1/3-(2/3).*cos(wave_angle)).*((wave_angle >=0) & (wave_angle <= pi/2)) +...
((log(B/T))^-1).*(0.87/CB).^(1+Fn) .*(wave_angle==pi)&(vel_nave>V_G(freq_wave)./2)&(Fn_rel(freq_wave)>=0.12) +...
((log(B/T))^-1).*(0.87/CB) .*(wave_angle==pi)&(vel_nave<V_G(freq_wave)./2)+...
((log(B/T))^-1).*(0.87/CB) .*(wave_angle==pi)&(Fn_rel(freq_wave)<0.12);
K>> a1(5,2.09)
ans =
logical
0
I have just tried to evaluate my function at the location (pi <= wave_angle < pi/2) by using, for example, wave_angle=2.09. In fact, since the function is not defined there the answer is 0. Instead, I need that the code takes the value of the a1 function from the range (wave_angle >=0) & (wave_angle <= pi/2) and interpolate it with the same a1 fucntion considered at the location (wave_angle==pi), in order to find the related value for a wave_angle between pi and pi/2.
Please supply a complete code where all the variables and functions are defined.
In your definition of a1 from above, B, T, CB, Fn, vel_nave, function V_G, function Fn_rel are missing.
%this is the code
B=22.5; T=7; CB=0.59; Fn=0.2; vel_nave=10.28;
V_G=@(freq_wave) g/(2*freq_wave);
Fn_rel=@(freq_wave) (vel_nave-V_G(freq_wave)/2)/(g*Lpp)^0.5;
a1=@(freq_wave,wave_angle) ((log(B/T))^-1).*(0.87/CB).^((1+Fn).*cos(wave_angle)).*(1/3-(2/3).*cos(wave_angle)).*((wave_angle >=0) & (wave_angle <= pi/2)) +...
((log(B/T))^-1).*(0.87/CB).^(1+Fn) .*(wave_angle==pi)&(vel_nave>V_G(freq_wave)./2)&(Fn_rel(freq_wave)>=0.12) +...
((log(B/T))^-1).*(0.87/CB) .*(wave_angle==pi)&(vel_nave<V_G(freq_wave)./2)+...
((log(B/T))^-1).*(0.87/CB) .*(wave_angle==pi)&(Fn_rel(freq_wave)<0.12);
As you can see, none of the four conditions is satisfied for your inputs. Thus the result is logical 0.
(Note that I added additional brackets around your conditions).
Extrapolating values for a1 over the limits where a1 is defined is completely arbitrary and thus nonsense.
B=22.5; T=7; CB=0.59; Fn=0.2; vel_nave=10.28; g=1;Lpp=1;
V_G=@(freq_wave) g/(2*freq_wave);
Fn_rel=@(freq_wave) (vel_nave-V_G(freq_wave)/2)/(g*Lpp)^0.5;
a1=@(freq_wave,wave_angle) ((log(B/T))^-1).*(0.87/CB).^((1+Fn).*cos(wave_angle)).*(1/3-(2/3).*cos(wave_angle)).*((wave_angle >=0) & (wave_angle <= pi/2)) +...
((log(B/T))^-1).*(0.87/CB).^(1+Fn) .*((wave_angle==pi)&(vel_nave>V_G(freq_wave)./2)&(Fn_rel(freq_wave)>=0.12)) +...
((log(B/T))^-1).*(0.87/CB) .*((wave_angle==pi)&(vel_nave<V_G(freq_wave)./2))+...
((log(B/T))^-1).*(0.87/CB) .*((wave_angle==pi)&(Fn_rel(freq_wave)<0.12));
freq_wave = 5;
wave_angle = 2.09;
((wave_angle >=0) & (wave_angle <= pi/2))
ans = logical
0
((wave_angle==pi)&(vel_nave>V_G(freq_wave)./2)&(Fn_rel(freq_wave)>=0.12))
ans = logical
0
((wave_angle==pi)&(vel_nave<V_G(freq_wave)./2))
ans = logical
0
((wave_angle==pi)&(Fn_rel(freq_wave)<0.12))
ans = logical
0
a1(5,2.09)
ans = 0
Hi Torsten, thanks for you support. Your results are exactlay what the code do at this moment: I need to fill the empty space where the function a1 is not defined by interporlating its defintion between
((log(B/T))^-1).*(0.87/CB).^((1+Fn).*cos(wave_angle)).*(1/3-(2/3).*cos(wave_angle))
valid only for
((wave_angle >=0) & (wave_angle <= pi/2))
and
((log(B/T))^-1).*(0.87/CB).^(1+Fn) .*(vel_nave>V_G(freq_wave)./2)&(Fn_rel(freq_wave)>=0.12)) +...
((log(B/T))^-1).*(0.87/CB) .*((vel_nave<V_G(freq_wave)./2))+...
((log(B/T))^-1).*(0.87/CB) .*((Fn_rel(freq_wave)<0.12));
valid only for
(wave_angle==pi)
Torsten
Torsten il 29 Nov 2023
Modificato: Torsten il 29 Nov 2023
And what should be the "interpolating" function ? It's completely arbitrary at the moment.

Accedi per commentare.

Risposte (1)

Hi Gianluca,
I understand that you are trying to evaluate a semantic relation, given the variable values. In the illustrated case, the equation to be used changes according to the range of the input.
It seems like you have a piecewise function defined over different ranges of wave_angle. In MATLAB, you can use the if, elseif, and else statements to define a piecewise function. The if statements can be used to check the conditions for each range and evaluate the corresponding expression.
Here is a code snippet to help you with the same:
function result = your_function(freq_wave, wave_angle, B, T, CB, Fn, vel_nave)
if wave_angle >= 0 && wave_angle < pi/2
result = ((log(B/T))^-1) .* (0.87/CB).^((1+Fn).*cos(wave_angle)) .* (1/3 - (2/3) .* cos(wave_angle));
elseif wave_angle == pi && vel_nave > V_G(freq_wave)/2 && Fn_rel(freq_wave) >= 0.12
result = ((log(B/T))^-1) .* (0.87/CB).^(1+Fn);
elseif wave_angle == pi && vel_nave < V_G(freq_wave)/2
result = ((log(B/T))^-1) .* (0.87/CB);
elseif wave_angle == pi && Fn_rel(freq_wave) < 0.12
result = ((log(B/T))^-1) .* (0.87/CB) .* (wave_angle == pi) .* (Fn_rel(freq_wave) < 0.12);
else
% Define default behavior if none of the conditions are met
result = NaN; % or any default value you prefer
end
end
This function checks the conditions for each range of wave_angle and evaluates the corresponding expression. You can adjust the conditions and expressions based on your specific requirements. The result variable holds the final value of the function for a given set of input parameters.
Since the signature and description of functions "Fn_rel" and others were not provided, I assume that they work as expected.
Hope this helps.
Regards,
Nipun

Categorie

Scopri di più su Interpolation in Centro assistenza e File Exchange

Prodotti

Release

R2018b

Richiesto:

il 29 Nov 2023

Risposto:

il 21 Dic 2023

Community Treasure Hunt

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

Start Hunting!

Translated by