Clear Filters
Clear Filters

How to assign a value to a variable in an equation

9 views (last 30 days)
Hi
I have an equation and I want to assign a value to its variable
I wrote the code below but it didn't change after running the code what can I do
Thanks
My code
clc
clear all
close all
warning off
zafar_queue =readtable('zafar_queue.xlsx');
Y = zafar_queue.nVehContrib;
data = Y';
x_train = floor(0.9*numel(data));
dataTrain =data(1:x_train);
n = length(dataTrain);
u = 0.1* randn(n,1) ;
% Import mydata
Opt = arxOptions;
Opt.InitialCondition = 'estimate';
arx30 = @(z)ar(dataTrain,[30], Opt)
z = Y(end)
frcast = arx30(z)
The result after compiling code is above without caculating z in it

Answers (1)

Ishu
Ishu on 5 May 2024
Edited: Ishu on 5 May 2024
Hi Arash,
I understand that your "dataTrain" expression contains a symbolic variable "z" which you want to replace it with some numeric value. As "dataTrain" is not a function of "z", its just an expression containing symbol "z".
The function handle "arx30 = @(z)ar(dataTrain,[30], Opt)" creates an anonymous function "arx30" that, when called, should execute "ar(dataTrain,[30], Opt)". Importantly, this captures the current value of dataTrain, which is the symbolic expression with "z". It does not create a dynamic link back to the symbolic variable "z".
When you assign z = Y(end);, you are overwriting the symbolic "z" with a numeric value. This does not retroactively change "dataTrain" or affect the function handle "arx30", because "dataTrain" was already defined in terms of the symbolic "z", and "arx30" captured "dataTrain's" symbolic form.
Calling "arx30" after "z" has been set to Y(end) does not substitute z into the original symbolic expression within "dataTrain". The function "ar" is called with the original symbolic expression, not with "z" substituted as Y(end), resulting in a symbolic output rather than a numeric one.
In order to substitute "z" with a numeric value and then compute the "arx30", you need to ensure that the substitution happens before or within the function call. Here's a way to achieve this by making "dataTrain" as a function of "z":
function y = ar(dataTrain,z)
y = dataTrain(z) + 30;
end
syms z;
dataTrain = @(z) z + z^2 + 2*z; % Assuming value to be z+z^2+2*z
arx30 = @(z)ar(dataTrain, z);
z = 4; % taken some random value as i dont have Y(end)
frcast = arx30(z)
frcast = 58

Community Treasure Hunt

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

Start Hunting!